错误:“在写入日志信息时,进程无法访问文件,因为它正由另一个进程使用” [英] Error : “The process cannot access the file '' because it is being used by another process” while writing the log information

查看:85
本文介绍了错误:“在写入日志信息时,进程无法访问文件,因为它正由另一个进程使用”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public static void WriteLogFile(string message)
       {
           Console.WriteLine(message);

           if (File.Exists("C:\\log.txt"))
           {
               using (StreamWriter SWriter = File.AppendText("C:\\log.txt"))
               {
                   SWriter.WriteLine(message);
               }
           }
           else
           {
               using (StreamWriter SWriter = File.CreateText("C:\\log.txt"))
               {

                   SWriter.WriteLine(message);
               }
           }
       }







这是我的代码。你能不能告诉我我提到的错误的原因。使用语句通常在对象超出范围后处理它,但是我得到了错误。




This is my code. Could you please anyone tell me the reason for the error which i mentioned. Using statement usually dispose the object once it went out of scope, but i am getting the error.

推荐答案

这是一个非常常见的问题。请在我过去的答案中查看我的建议:如何压缩错误'它已被另一个进程使用' vb.net [ ^ ]。



-SA
This is a very usual problem. Please see my recommendations in my past answer: how to compress the error 'it is already used by another process' in vb.net[^].

—SA


这篇关于错误:“在写入日志信息时,进程无法访问文件,因为它正由另一个进程使用”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆