简体   繁体   English

启动 StreamWriter 时抛出异常

[英]Exception thrown when starting StreamWriter

Every 24 hours, my code auto-generates a .csv-file, writes it temporarily to an Azure directory, and finally deletes it after operating on it.每 24 小时,我的代码自动生成一个 .csv 文件,将其临时写入 Azure 目录,最后在对其进行操作后将其删除。

It succeeds, but from the logs I can see, that an exception is thrown.它成功了,但从我可以看到的日志中,抛出了一个异常。

Exception:例外:

"The process cannot access the file 'D:\\home\\site\\wwwroot\\myFile.csv' because it is being used by another process." “该进程无法访问文件 'D:\\home\\site\\wwwroot\\myFile.csv',因为它正被另一个进程使用。”

The log points to these two lines of code, where I simply specify the directory and file-name and then start a StreamWriter:日志指向这两行代码,我只是指定目录和文件名,然后启动一个StreamWriter:

string filePath = Environment.CurrentDirectory + "\\myFile.csv"; //Specify where to create csv on hosted Azure server (not locally)
using (var w = new StreamWriter(filePath, false, new UTF8Encoding(false))) //Exception is thrown here
{
    //more code
}

I am very confused, how the two above lines can result in that exception, especially since the file is always deleted after upload.我很困惑,上面两行如何导致该异常,特别是因为文件在上传后总是被删除。

For my particular case, the problem was that the StreamWriter code was executed twice , instead of the intended once .对于我的特殊情况,问题是StreamWriter代码执行了两次,而不是预期的一次 Thanks to user TheGeneral for guiding me in the right direction in the comments.感谢用户 TheGeneral 在评论中指导我朝着正确的方向前进。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM