[英]The process cannot acces the file as it is being used by another process C#
I have a .Net console application. 我有一个.Net控制台应用程序。 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. 我对发送电子邮件任务有一个类似的catch stmt,当它失败时,它将尝试写入我在电子邮件任务中附加的相同错误文件。 Once i removed the catch logic the issue is resolved. 一旦我删除了catch逻辑,问题就解决了。 Whew!! 呼! I know.. It's a stupid mistake! 我知道..这是一个愚蠢的错误! :( Learnt something from this mistake :(从这个错误中学到了一些东西
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.