Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read a text file from local folder

Tags:

c#

file

file-io

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??

like image 419
Dabiddo Avatar asked Feb 18 '10 20:02

Dabiddo


People also ask

How do I read a text file on a PC?

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....

How do I access a text file?

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.


1 Answers

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");
like image 195
Stan R. Avatar answered Sep 28 '22 07:09

Stan R.