C# 进程无法访问文件 'XYZ' 因为它正被另一个进程使用
C# The process cannot access file 'XYZ' because it is being used by another process
最近几天我一直在与这个问题作斗争,当我在我的开发机器上时它工作正常,但在客户端上它显示这个错误。
现在这是我的代码,它似乎显示了错误,所以任何帮助或指导都会很棒,提前谢谢你。
private void document()
{
StreamWriter sWrite = new StreamWriter("C:\Demo\index.html");
//LOTS OF SWRITE LINES HERE
sWrite.Close();
System.Diagnostics.Process.Start("C:\Demo\index.html");
}
所以我不知道如果我 运行 这个方法两次,它总是告诉我文件已经被另一个进程使用了。
例如,在尝试从 Process.Start
打开文件之前,您可以执行以下操作
var path = @"C:\Demo\index.html";
using (FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write))
using (StreamWriter sw = new StreamWriter(fs))
{
sw.WriteLine("Your contents to be written go here");
}
System.Diagnostics.Process.Start(path);
其中一些取决于确切的行为。这可能有几个原因:例如,可能是由于异常。以下代码将产生您所描述的异常。
for (int i = 0; i < 10; i++)
{
const string path = @"[path].xml";
try
{
// After the first exception, this call will start throwing
// an exception to the effect that the file is in use
StreamWriter sWrite = new StreamWriter(path, true);
// The first time I run this exception will be raised
throw new Exception();
// Close will never get called and now I'll get an exception saying that the file is still in use
// when I try to open it again. That's because the file lock was never released due to the exception
sWrite.Close();
}
catch (Exception e)
{
}
//LOTS OF SWRITE LINES HERE
Process.Start(path);
}
一个 "using" 块将解决这个问题,因为它等同于:
try
{
//...
}
finally
{
stream.Dispose();
}
在您的代码上下文中,如果您正在编写一大堆行,那么实际上 考虑是否(以及何时)调用 Flush 是有意义的在某一点。问题是写入是否应该 "all or none" - 即如果发生异常,您是否仍要写入前几行?如果没有,只需使用 "using" 块 - 它会在 "Dispose." 末尾调用 "Flush" 一次,否则,您可以更早地调用 "Flush" 。例如:
using (StreamWriter sw = new StreamWriter(...))
{
sw.WriteLine("your content");
// A bunch of writes
// Commit everything we've written so far to disc
// ONLY do this if you could stop writing at this point and have the file be in a valid state.
sw.Flush();
sw.WriteLine("more content");
// More writes
} // Now the using calls Dispose(), which calls Flush() again
如果您在多个线程上执行此操作(尤其是在执行大量写入操作时),则可能存在一个很大的错误。如果一个线程调用您的方法并开始写入文件,然后另一个线程也调用它并尝试开始写入文件,则第二个线程的调用将失败,因为第一个线程仍在使用该文件。如果是这种情况,您将需要使用某种锁来确保线程 "take turns" 写入文件。
最近几天我一直在与这个问题作斗争,当我在我的开发机器上时它工作正常,但在客户端上它显示这个错误。
现在这是我的代码,它似乎显示了错误,所以任何帮助或指导都会很棒,提前谢谢你。
private void document()
{
StreamWriter sWrite = new StreamWriter("C:\Demo\index.html");
//LOTS OF SWRITE LINES HERE
sWrite.Close();
System.Diagnostics.Process.Start("C:\Demo\index.html");
}
所以我不知道如果我 运行 这个方法两次,它总是告诉我文件已经被另一个进程使用了。
例如,在尝试从 Process.Start
打开文件之前,您可以执行以下操作var path = @"C:\Demo\index.html";
using (FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write))
using (StreamWriter sw = new StreamWriter(fs))
{
sw.WriteLine("Your contents to be written go here");
}
System.Diagnostics.Process.Start(path);
其中一些取决于确切的行为。这可能有几个原因:例如,可能是由于异常。以下代码将产生您所描述的异常。
for (int i = 0; i < 10; i++)
{
const string path = @"[path].xml";
try
{
// After the first exception, this call will start throwing
// an exception to the effect that the file is in use
StreamWriter sWrite = new StreamWriter(path, true);
// The first time I run this exception will be raised
throw new Exception();
// Close will never get called and now I'll get an exception saying that the file is still in use
// when I try to open it again. That's because the file lock was never released due to the exception
sWrite.Close();
}
catch (Exception e)
{
}
//LOTS OF SWRITE LINES HERE
Process.Start(path);
}
一个 "using" 块将解决这个问题,因为它等同于:
try
{
//...
}
finally
{
stream.Dispose();
}
在您的代码上下文中,如果您正在编写一大堆行,那么实际上 考虑是否(以及何时)调用 Flush 是有意义的在某一点。问题是写入是否应该 "all or none" - 即如果发生异常,您是否仍要写入前几行?如果没有,只需使用 "using" 块 - 它会在 "Dispose." 末尾调用 "Flush" 一次,否则,您可以更早地调用 "Flush" 。例如:
using (StreamWriter sw = new StreamWriter(...))
{
sw.WriteLine("your content");
// A bunch of writes
// Commit everything we've written so far to disc
// ONLY do this if you could stop writing at this point and have the file be in a valid state.
sw.Flush();
sw.WriteLine("more content");
// More writes
} // Now the using calls Dispose(), which calls Flush() again
如果您在多个线程上执行此操作(尤其是在执行大量写入操作时),则可能存在一个很大的错误。如果一个线程调用您的方法并开始写入文件,然后另一个线程也调用它并尝试开始写入文件,则第二个线程的调用将失败,因为第一个线程仍在使用该文件。如果是这种情况,您将需要使用某种锁来确保线程 "take turns" 写入文件。