在asp.net中写入文本文件时出错 [英] Error occure while writing to the text file in asp.net

查看:97
本文介绍了在asp.net中写入文本文件时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的专家,



我在做asp.net项目。我在写文件到文件时遇到错误...



这里是下面的c#代码..



Dear Experts,

I am doing ans asp.net project. and i got an error while write text to the file...

here is the c# code below..

public static void FileWrite(string _log_Code,string _Log_Msg)
       {
           string _File_Path = Dir_Path(_log_Code);
           string _Error_log = string.Empty;

           if (!File.Exists(_File_Path))
           {
               File.Create(_File_Path);
           }

           using (StreamWriter sw = new StreamWriter(@_File_Path, true))
           {

               _Error_log = "***********************************************************************************************************************************************" + Environment.NewLine ;
               _Error_log += _Log_Msg + Environment.NewLine;
               _Error_log += "***********************************************************************************************************************************************";

               sw.Write(Environment.NewLine + _Error_log);
               sw.Flush();
               sw.Close();
               sw.Dispose();

           }



       }





以下是我得到的以下错误...





Here is the following Error that i got...

The process cannot access the file 'D:\XXX\XXX\FileStore\Error_Log\Error_Log_07-01-2014.txt' because it is being used by another process.





请帮我解决这个问题..





谢谢和问候,



Dileep



Please help me to solve this..


Thanks and regards,

Dileep

推荐答案

错误信息非常清楚:文件正在使用中。并且(如果您查看文档)这是您的代码正在执行锁定...

File.Create返回一个文件流,因此它会打开并锁定文件以进行写入。然后你尝试再次打开同一个文件,系统自然会抱怨。

我会使用File.AppendAllText而不是所有这些 - 它会在需要时创建文件,并添加数据没有任何进一步的锁定。
The error message is pretty clear: the file is in use. And (if you look at the documentation) it's your code that is doing the locking...
File.Create returns a filestream, so it opens and locks the file for writing. You then try to open the same file again and naturally enough the system complains.
I would use File.AppendAllText instead of all of that - it will create the file if it needs to, and add the data without any further locking.


请参阅 - 使用File.Create后其他进程正在使用的文件( ) [ ^ ]

Refer - File being used by another process after using File.Create()[^]
Quote:

File.Create方法创建文件并在文件上打开FileStream。所以你的文件已经打开了。你根本不需要file.Create方法:



The File.Create method creates the file and opens a FileStream on the file. So your file is already open. You don't really need the file.Create method at all:

string filePath = @"c:\somefilename.txt";
using (StreamWriter sw = new StreamWriter(filePath, true))
{
    //write to the file
}



如果文件存在,StreamWriter构造函数中的布尔值将导致附加内容。


The boolean in the StreamWriter constructor will cause the contents to be appended if the file exists.


您好Dilzz,



尝试参考 here [ ^ ],

link2 [ ^ ]

link3 [ ^ ]

这可以帮到你。



希望这对你有所帮助。



问候,

RK
Hi Dilzz,

Try referring here[^],
link2[^]
link3[^]
this could help you out.

Hope this helps you a bit.

Regards,
RK


这篇关于在asp.net中写入文本文件时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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