简体   繁体   中英

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.

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."

The log points to these two lines of code, where I simply specify the directory and file-name and then start a 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 . Thanks to user TheGeneral for guiding me in the right direction in the comments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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