【发布时间】:2014-05-08 15:15:31
【问题描述】:
我正在尝试使用以下代码在 C# 应用程序中读取对文本文件的写入
using (System.IO.StreamReader rd = new System.IO.StreamReader(path))
{
using (System.IO.StreamWriter wr = new System.IO.StreamWriter(path))
{
string line = null;
while ((line = rd.ReadLine()) != null)
{
if (String.Compare(line, active_user) == 0)
{
//do nothing
}
else
{
wr.WriteLine(active_user);
}
}
}
}
即使我使用一次性物品,仍然给我报错
该进程无法访问文件“路径”,因为它正被另一个进程使用。
我是不是哪里出错了?
【问题讨论】:
-
我正在检查所有内容,但没有找到任何使用该文件的应用程序。有没有办法强制所有进程以编程方式释放文件?
-
你正在同时读取和写入文件。
-
这是错误的方法。写入一个新的临时文件,然后删除旧文件并将新文件重命名为旧名称。
标签: c# visual-studio-2012 text-files