14,603,886 members
Rate this:
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.
string line = string.Empty;
{
sb.Append(line);
sb.Append(Environment.NewLine);
}

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
RDBurmon 23-Jan-12 8:16am

What is "sb" ?

Rate this:

Solution 1

using System.IO;

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:

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
jaideepsinh 27-Jan-12 9:59am

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.