【问题标题】:C# The process cannot access file 'XYZ' because it is being used by another processC#进程无法访问文件'XYZ',因为它正被另一个进程使用
【发布时间】:2016-12-24 12:52:26
【问题描述】:

最近几天我一直在解决这个问题,当我在我的开发机器上时它工作正常,但在客户端上却显示此错误。

现在这是我拥有的似乎显示错误的代码,因此任何帮助或指导都会令人惊叹,提前谢谢你。

 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");
 }

所以我不知道如果我运行此方法两次,它一直告诉我该文件已被另一个进程使用。

【问题讨论】:

  • 尝试将StreamWriter code 包裹在using( ) {} 周围
  • a) 你能给出异常的堆栈跟踪吗? b)您确定该方法中永远不会发生异常吗? c) 这可以从多个线程中调用吗?
  • @VBenavides,尝试发布答案中的代码,并在目标服务器上进行两种方式的测试。一个注释掉Flush() 代码,另一个注释掉Flush() 代码。
  • 谢谢大家的回答,我现在就试试!
  • 如果您在多线程环境中运行,请记住您必须使用某种锁来防止竞争条件。

标签: c# html streamwriter process.start


【解决方案1】:

例如,在尝试从 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);

【讨论】:

  • using handles the auto-disposing of the created objects flush 立即写入文件.. 阅读using and Flush() 方法
  • Dispose 已经为 StreamWriter 调用了 Flush,因此不必同时使用“using”块和调用“flush”。
  • 我不是在问为什么“使用”——这是完全明智的。我的问题是为什么你调用“flush” - 在代码示例的上下文中,假设 Dispose() 已经调用它是不必要的。
  • 我第二个@EJoshuaS 的问题/评论:Flush 在这种情况下是不必要的,因为Using 会调用Dispose,而后者又会调用Flush,从而有效地进行调用到Flush冗余。
  • 我每天都遇到这样的情况,在本地机器上一切正常,一旦在服务器上,文件已经在使用中就会发生错误,我放入刷新的原因是因为你不能假设该对象将被立即处理,也不能假设所有内容都将被隐式刷新.. op 可以使用 Flush() 测试它或注释掉刷新.. 我使用刷新是因为不依赖的经验假设
【解决方案2】:

其中一些取决于确切的行为。这可能有几个原因:例如,可能是由于异常。以下代码将产生您描述的异常。

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);
        }

“使用”块将解决此问题,因为它相当于:

try
{
   //...
}
finally
{
   stream.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

如果您在多个线程上执行此操作(尤其是在执行大量写入操作时),则可能存在一个很大的错误。如果一个线程调用您的方法并开始写入文件,然后另一个线程也调用它并尝试开始写入文件,则第二个线程的调用将失败,因为第一个线程仍在使用该文件。如果是这种情况,您需要使用某种锁来确保线程“轮流”写入文件。

【讨论】:

    猜你喜欢
    • 2011-07-05
    • 2011-04-17
    • 2013-06-28
    • 1970-01-01
    • 2011-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多