0

I have a txt file located in the application folder, in visual studio i just write "File.read("doc.txt")", however i would like to place the txt file outside of the applications folder

e.g right now its located in c:\Desktop\Folder\application\doc.txt and i want to place the doc.txt inside c:\desktop\folder and read it from there without specifying the exact path of the txt file as folder might be moved to another drive e.g inside D drive D:\desktop\folder

if the user moves the folder and my hard code is "file.read("C:\desktop\folder\doc.txt")" its going to throw an error, so how is there anyway to read a file that is one location above the applications main Folder?

wren
  • 373
  • 5
  • 15

1 Answers1

1
File.Read(@"..\doc.txt");

Will achieve this, the reason to add @ at the start is to show that you're writing a string literal which will allow you to use '\' without needing to escape it. the ".." part is how you tell the document to go into the parent directory.

DMck
  • 50
  • 4
  • Hm what is the "\" used for actually if we cant write it in the string? – wren Apr 24 '19 at 12:41
  • 1
    @guyguy, it's an escape character as well as a folder separator. Without the '@' you would need to write `"..\\doc.txt"`. – spodger Apr 24 '19 at 12:51
  • 2
    You can write it in a string but without the `@` in front of the string it has a special meaning, it "escapes" the next character, either adding or taking away meaning. For instance, `n` in a string is just the letter n, but if you write `\n`, it means *newline*, the two characters gets replaced by one. To actually write a backslash into a string, without using `@` in front of it you need to escape the backslash, so you would have to write `"..\\doc.txt"`. – Lasse V. Karlsen Apr 24 '19 at 12:51