Click here to Skip to main content
14,603,886 members
Rate this:
Please Sign up or sign in to vote.
See more:
I add text file in my window application.And i want to read and write that text file.
I write into that text file when i start my application but when i stop my application and when i start again my project then all data remove from my text file.
I use this code for reading text file.And every time not go in to while loop.
Can any one solve this.
StreamReader objStreamReader;
objStreamReader = File.OpenText("Manual_pro_ID.txt");
string line = string.Empty;
while ((line = objStreamReader.ReadLine()) != null)
{
sb.Append(line);
sb.Append(Environment.NewLine);
}
objStreamReader.Close();

I just want to read and write text file which is store in my solution explorer.
So done all function but i just have problem in file path i am not get correct path of my text file.So if you know then tell me which path i take for that file.
Posted
Updated 27-Jan-12 3:59am
v2
Comments
RDBurmon 23-Jan-12 8:16am
   
What is "sb" ?
Rate this:
Please Sign up or sign in to vote.

Solution 1

using System.IO;

StreamReader objReader = new StreamReader(Application.StartupPath + "\\text.txt");
string content = objReader.ReadToEnd();
   
Comments
jaideepsinh 23-Jan-12 8:28am
   
Thank's for ans.
By this method data will not at text file.
   
My 5. I did not notice that you uses StartupPath, though you had a bug. So, I added my solution where I explain another method of getting the executable path, more universal way (not using Forms) and added a note with resource option, please see.
--SA
Rate this:
Please Sign up or sign in to vote.

Solution 2

There is one additional problem related to the file path.

Actually, there are no situations when hard-coding of the file name can be useful. The problem with this code is this: it assumes that the working directory at the moment of opening the file will be the one where your text file is located. This is wrong: the user can run any application starting from any directory. In this case, the file will not be found.

If the file is read-only, it's possible that you put it in your output directory, the same one where your executable file is located. So, you need to calculate this directory. Here is how:

string exeDirectory = System.IO.Path.GetDirectoryName(
   System.Reflection.Assembly.GetEntryAssembly().Location);


Another option is to add the file to the project but embed it in the executable file as a resource. In this case, you don't have to read anything. Visual Studio will create auto-generated source file with the static string property already containing the content of the text file. The file will not be copied to output directory as executable file contains it content.

—SA
   
v2
Comments
jaideepsinh 27-Jan-12 9:59am
   
Thank's for reply.
I just want to read and write text file which is store in my solution explorer.
So done all function but i just have problem in file path i am not get correct path of my text file.So if you know then tell me which path i take for that file.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100