I want to read a text file from my local directory, I added the text file to my c# solution, so it would get copied at deployment.. but how do i open it? I've been searching but all the examples assume I have a C:\textfile.txt:
I tried just reading the file
if (File.Exists("testfile.txt"))
{
return true;
}
That didn't work. Then I tried:
if (File.Exists(@"\\TextConsole\testfile.txt"))
{
return true;
}
but still wont open it.. any ideas??
You can open a TXT file with any text editor and most popular web browsers. In Windows, you can open a TXT file with Microsoft Notepad or Microsoft WordPad, both of which come included with Windows. To open a TXT file with Notepad, select File → Open....
To read from a text fileUse the ReadAllText method of the My. Computer. FileSystem object to read the contents of a text file into a string, supplying the path. The following example reads the contents of test.
Just because you added it to your solution doesn't mean the file gets placed into your output Build directory. If you want to use relative path, make sure your TextFile is copied during build to the output directory. To do this, in solution explorer go to properties of the text file and set Copy to Output Directory
to Always
or Copy if newer
Then you can use
File.Open("textfile.txt");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With