在文本文件中写入数据时出现问题 [英] Problem in writing data in text file

查看:80
本文介绍了在文本文件中写入数据时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我正在使用C#开发一个Windows软件,我必须创建一个文本文件,并将其保存到我在路径下面给出的软件根路径中。\\



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





问题是每次我写数据并尝试将其保存在我的文本文件中但它覆盖了我不喜欢的文件不想覆盖。任何人都可以告诉我如何停止覆盖。





谢谢和问候

Indrajit Dasgupta

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

推荐答案

我的建议将帮助您解决问题,请阅读:如何:打开并附加到日志文件 [ ^ ]
My suggestion which will help you to solve the problem is to read this: How to: Open and Append to a Log File[^]


如果您需要始终将数据附加到同一文件,那么Maciej已经为您提供了解决方案。<另一方面,如果您需要将每个会话信息存储到另一个文件中,那么每次执行应用程序时都可以生成一个具有不同名称的新文件(您可以使用持久计数器,或者基于程序执行日期和时间的名称,...)。

最后,如果你想修改文件的现有内容,那么你首先要阅读它,修改内容记忆然后将修改后的内容写回原始文件。
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.


这篇关于在文本文件中写入数据时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆