Click here to Skip to main content
14,775,260 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All
I am developing a windows software in C# where I have to create text file and save it to the software root path for that I had given below path\

string datapath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\MyTextFile.txt";


Problem is every time I am writing data and try to save it in my text file but it is overwriting the file which I don't want to overwrite. Can any one please tell me how can I stop overwriting .


Thanks & Regards
Indrajit Dasgupta
Posted
Comments
Pheonyx 19-Dec-13 4:59am
   
Where is your code that writes the file?
I suspect you are ignoring the "append" flag that can be passed when creating a file stream.

My suggestion which will help you to solve the problem is to read this: How to: Open and Append to a Log File[^]
   
Comments
CPallini 19-Dec-13 5:07am
   
5.
Maciej Los 19-Dec-13 5:07am
   
Thank you, Carlo ;)
Karthik_Mahalingam 19-Dec-13 5:25am
   
nice link...
Maciej Los 19-Dec-13 5:35am
   
Thank you ;)
If you need to always append data to the same file then Maciej already gave you the solution.
On the other hand, if you need to store each session info into a different file then you may generate a new file with different name each time your application is executed (you may use a persistent counter for this, or a name based on program date and time of execution, ...).
Finally, if you want to modify the existing content of the file then you have first to read it, modify the content in memory and then write back the modified content to the original file.
   
Comments
Maciej Los 19-Dec-13 5:13am
   
Good advice ;)
+5!

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




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900