【问题标题】:the process cannot access the file because it is being used by another process ioexception该进程无法访问该文件,因为它正被另一个进程使用 ioexception
【发布时间】:2020-03-11 11:10:21
【问题描述】:

不太清楚为什么我已经使用了usingfs.Close()file.Close(),但是我第二次运行这些代码时仍然遇到这个错误。

using (FileStream fs = new FileStream(filePath, FileMode.Create))
    {

        using (StreamWriter file = new StreamWriter(fs))
        {
            // Display header
            string header = string.Format("{0,-40} {1,-12} {2,-15} {3,-8}",
                                          "Product Name", "Unit Price", "Quantity", "Total");
            file.WriteLine(header);

            foreach (var item in shoppingCart2)
            {
                file.WriteLine("{0,-40} {1,-12} {2,-15} {3,-8}", item.ProductName,
                    Utility.FormatAmount(item.UnitPrice), item.QuantityOrder,
                    Utility.FormatAmount(item.TotalAmount));

                table.AddRow(item.ProductName, Utility.FormatAmount(item.UnitPrice),
                    item.QuantityOrder, Utility.FormatAmount(item.TotalAmount));
            }

            table.Options.EnableCount = false;
            table.Write();
            file.Close();
        }
        fs.Close();
    }

【问题讨论】:

  • 问题不在这里......你以后如何处理文件?你是在另一个进程中打开它吗? (题外话:Dispose 时不需要 Close)
  • 尝试并使用进程资源管理器找出实际上是哪个进程锁定了您的文件。这真的是你的程序吗?
  • 您是否在 anther 编辑器中打开了该文件以进行调试?
  • 我使用的是我之前声明的filePath 吗? var filePath = "c:/file/Receipt.txt";
  • @tymtam。不,我从记事本中关闭了所有 Receipt.txt

标签: c# filestream streamwriter


【解决方案1】:

可能您的文件仍处于锁定状态。 fs.close 或 using 语句不会立即释放文件,这可能需要一些时间。

我了解到您下次想阅读此文件,而这正是您收到错误消息的时候。所以在下次读取文件之前可以试试:

while (true)
{
    try
    {
        read this file from this place
        break;
    }
    catch 
    { 
        Sleep(100);
    }
}

这不是完美的解决方案,但您可以通过此验证正在发生的事情,并更接近解决方案。

fs.close() 和 file.close() 是不必要的,因为 using 语句会为您关闭这些文件。

【讨论】:

    【解决方案2】:

    我认为问题出在其他地方(您可能在单独的编辑器中打开了文件)。

    尽管如此,我建议在这种情况下使用WriteAllLines。它将最小化文件打开的时间。

    请注意WriteAllLines'如果目标文件已经存在,则覆盖'

    const string template = "{0,-40} {1,-12} {2,-15} {3,-8}";
    var lines = new List<string>();
    
    lines.Add(string.Format(template, "Product Name", "Unit Price", "Quantity", "Total"));
    foreach (var item in shoppingCart2)
    {
        lines.Add(string.Format(template, item.ProductName,
            Utility.FormatAmount(item.UnitPrice), item.QuantityOrder,
            Utility.FormatAmount(item.TotalAmount)));
    
        table.AddRow(item.ProductName, Utility.FormatAmount(item.UnitPrice),
            item.QuantityOrder, Utility.FormatAmount(item.TotalAmount));
    }
    
    table.Options.EnableCount = false;
    table.Write();
    File.WriteAllLines(filePath, lines);
    

    【讨论】:

      【解决方案3】:

      我意识到我在原始代码之后附加了文件:

       Attachment attachment;
       attachment = new Attachment(filePath);
      

      所以,我的修复是处理它,然后错误就消失了。

      attachment.Dispose();
      

      【讨论】:

        猜你喜欢
        • 2018-09-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-10
        相关资源
        最近更新 更多