0

Struggling with a small part to a recent assignment, we've been asked to provide only the .cs file during the hand in process of our assignment, however it also asks us to reference a .txt file. Without setting the exact path to my file (ie: c:\users\username\documents etc).

The program is console based as this is just an algorithm and structure class, but I'm still a little miffed on how to reference without having the reference be static, or the txt document being added to a solution.

I've considered asking for the directory path input at the start of the program, but I just wondered if there was a better way of doing it.

2 Answers 2

2

You've pretty much covered the options. If you can only provide that cs file then you can only:

  • Hard-code the location in the file as an absolute path - e.g. c:\testfile.txt
  • Hard-code the location as a relative path - e.g. ..\testfile.txt
  • Get the location as a command line argument
  • Make the console program interactive and prompt for the location after it starts
  • Get the location from an environment variable
Sign up to request clarification or add additional context in comments.

1 Comment

Okay well then I'll probably take the user input in that case, thanks for the swift help :)
0

Just read the file from the application's directory, for example using this:

using (var reader = new StreamReader("myfile.txt"))

2 Comments

We're using C# and as I said, we can only hand in the .cs file, not a solution folder. Thanks anyway :)
But the assignment is to "reference" a txt file. If you only can hand in a .cs file, your code can safely assume the .txt file is in the same folder.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.