简体   繁体   English

类型为“ System.IO.IOException”的未处理异常。该进程无法访问该文件,因为该文件正在被另一个进程使用

[英]An unhandled exception of type 'System.IO.IOException'.The process cannot access the file because it is being used by another process

I am trying to write a program to read records from a csv file, process them and write the results to a new file. 我正在尝试编写一个程序来从csv文件中读取记录,对其进行处理并将结果写入新文件中。 To do so I constructed a class called CsvWriter that handles writing of headers and appending rows as I read through the main csv file. 为此,我构造了一个名为CsvWriter的类,该类在我阅读主csv文件时处理标题的编写和附加行。 Initially I was working on it on my mac and I did not get any error whatsoever when using this class but when I switched to windows (to make a windows form application), I kept getting 最初,我在Mac上进行处理,使用此类时我没有收到任何错误,但是当我切换到Windows(以制作Windows窗体应用程序)时,我一直在

An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll mscorlib.dll中发生了'System.IO.IOException'类型的未处理异常

Additional information: The process cannot access the file 'C:\\Users\\user\\Documents\\correct-list.csv' because it is being used by another process. 附加信息:该进程无法访问文件'C:\\ Users \\ user \\ Documents \\ correct-list.csv',因为它正在被另一个进程使用。

The error I'm having is in the CsvWriter class in the following line: 我遇到的错误在CsvWriter类的以下行中:

File.WriteAllText(filename, builder.ToString());

I thought using System.IO.File.WriteAllText would flush and close the file automatically. 我以为使用System.IO.File.WriteAllText会自动刷新并关闭文件。 I cannot find any other source of the error. 我找不到该错误的任何其他来源。 Any help to resolve this error is appreciated. 感谢您提供任何解决此错误的帮助。

This is my code: 这是我的代码:

public class CsvWriter : StreamWriter
{
    private string filename;

    /// <summary>
    /// Initializes a new instance of the <see cref="T:USPSAddressVerification.CsvWriter"/> class.
    /// </summary>
    /// <param name="filename">Filename of the csv file to write on.</param>
    public CsvWriter(string filename) : base(filename)
    {
        this.filename = filename;
    }

    /// <summary>
    /// Writes the header in the csv file.
    /// </summary>
    /// <param name="headers">Array of Headers.</param>
    /// <param name="addHeaders">Add additional headers to the csv file.</param>
    public void WriteHeader(List<string> headers)
    {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < headers.Count; i++)
        {
            if (i < headers.Count - 1) builder.Append("\"" + headers[i] + "\",");
            else builder.Append("\"" + headers[i] + "\"\n");
        }
        File.WriteAllText(filename, builder.ToString());
    }

    /// <summary>
    /// Appends the row to the csv file.
    /// </summary>
    /// <param name="row">Row to be appended.</param>
    public void AppendRow(List<string> row)
    {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < row.Count; i++)
        {
            if (i < row.Count - 1) builder.Append("\"" + row[i] + "\",");
            else builder.Append("\"" + row[i] + "\"\n");
        }
        File.AppendAllText(filename, builder.ToString());
    }

}

You are deriving StreamWriter, this opens the file. 您在派生StreamWriter,这将打开文件。 Instead of using File.WriteAllText you should use the StreamWriter (base) methods to write your contents. 而不是使用File.WriteAllText,您应该使用StreamWriter(基本)方法来编写内容。 Or don't derive StreamWriter. 或不要派生StreamWriter。

Your class inherits from the System.IO.StreamWriter. 您的类继承自System.IO.StreamWriter。 When you are calling your constructor with the filename parameter you are calling the StreamWriter constructor wih this(filename) as well. 当使用filename参数调用构造函数时,也将使用this(filename)调用StreamWriter构造函数。 With that command you create an instance of the StreamWriter class that access the file as long you do not dispose or close it. 使用该命令,您可以创建StreamWriter类的实例,只要您不处理或关闭文件就可以访问该文件。 That is the reason why your File.WriteAllText command will fail every time. 这就是为什么File.WriteAllText命令每次都会失败的原因。 Is there any reason why your class inherits from the StreamWriter? 您的类是否从StreamWriter继承有什么原因? File.WriteAllText works without it. File.WriteAllText没有它就可以工作。

Update 1 更新1

Ups my answer was a little bit to late. Ups我的回答有点迟了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未处理的异常:System.IO.IOException:该进程无法访问文件“ xxx.nupkg”,因为它正在被另一个进程使用 - Unhandled exception: System.IO.IOException: The process cannot access the file 'xxx.nupkg' because it is being used by another process mscorlib.dll中发生类型&#39;System.IO.IOException&#39;的异常-该进程无法访问该文件,因为该文件正在被另一个进程使用 - Exception of type 'System.IO.IOException' occurred in mscorlib.dll - The process cannot access the file because it is being used by another process System.IO.IOException: &#39;进程无法访问文件“文件位置”,因为它正被另一个进程使用。 - System.IO.IOException: 'The process cannot access the file "file location"because it is being used by another process.' System.IO.IOException: &#39;进程无法访问文件 &#39;@.txt&#39;,因为它正被另一个进程使用。 - System.IO.IOException: 'The process cannot access the file '@.txt' because it is being used by another process.' System.IO.IOException:进程无法访问文件“somefile.txt”,因为它正被另一个进程使用 - System.IO.IOException: The process cannot access the file 'somefile.txt' because it is being used by another process System.IO.IOException:进程无法访问文件&#39;.txt&#39;,因为它正由另一个进程使用 - System.IO.IOException: The process cannot access the file '.txt' because it is being used by another process 错误:帮助D:System.IO.IOException:该进程无法访问文件,因为它正在被另一个进程使用 - Error: Help D: System.IO.IOException: The process cannot access the file because it is being used by another process System.IO.IOException: '进程无法访问该文件,因为它正被另一个进程使用 - System.IO.IOException: 'The process cannot access the file because it is being used by another process 此代码给出此异常“System.IO.IOException:'该进程无法访问该文件,因为它正在被另一个进程使用。” ” - this code give this exception “System.IO.IOException: 'The process cannot access the file because it is being used by another process.' ” “ System.IO.IOException:该进程无法访问文件&#39;C:\\ Test \\ test.txt&#39;,因为它正在被另一个进程使用” - “System.IO.IOException: The process cannot access the file 'C:\Test\test.txt' because it is being used by another process”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM