简体   繁体   中英

The process cannot acces the file as it is being used by another process C#

I have a .Net console application. I would like to catch any exceptions occur during the execution and write it to a text file. If there is any entry on the file before, I am getting

The process cannot acces the file as it is being used by another process.

I tried closing the connections as well below but still it's showing the same error.

catch (Exception ex)
{
using (System.IO.StreamWriter writeerror = new System.IO.StreamWriter(_txtError))
            {
                writeerror.WriteLine(ex.Message);
                writeerror.Flush();
                writeerror.Close();
             }
}

Note: At the start of the program execution I am deleting the file and regenerate if any error occurs.

Finally I found where was the issue. At the end of all the functions I had a send e-mail function which send the log file as attachment to a group. The problem is from my workstation I can't send emails. Once the deploy the code in server, e-mail task will function as it's supposed to do. I had a similar catch stmt for Send email task and when it fails it tries to write the same error file I'm ATTACHING in the email task. Once i removed the catch logic the issue is resolved. Whew!! I know.. It's a stupid mistake! :( Learnt something from this mistake

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