Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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..

C#
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.


Please help me to solve this..


Thanks and regards,

Dileep
Posted

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.
 
Share this answer
 
Refer - File being used by another process after using File.Create()[^]
Quote:
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:

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

The boolean in the StreamWriter constructor will cause the contents to be appended if the file exists.
 
Share this answer
 
Hi Dilzz,

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

Hope this helps you a bit.

Regards,
RK
 
Share this answer
 
v2
File.Create(_File_Path) will create, open and lock with no share the new file, so StreamWriter will find it inaccessible!
Do something like this:

C#
Stream file = File.Create(_File_Path);
StreamWriter sw = new StreamWriter(file);
 
Share this answer
 
Comments
dilzz 7-Jan-14 9:53am    
Thanks for ur comment....
But in the case of the file already exist...?? then how to handle..??
Kornfeld Eliyahu Peter 7-Jan-14 9:55am    
Do File.Open()...
http://msdn.microsoft.com/en-us/library/b9skfh7s(v=vs.110).aspx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900