Click here to Skip to main content
15,924,507 members
Please Sign up or sign in to vote.
4.00/5 (3 votes)
See more:
I am using the following code to save the data in the text file

C#
public void Logtext(string txt, string fn)
{
    try
    {
      StreamWriter sw = File.AppendText(fn);
      sw.WriteLine(txt);
    }
    catch {...}
    finally
    {
      sw.Flush();
      sw.Close();
      sw.Dispose();
    }
}

Sometimes File.AppendText(fn) statement throws an exception as:
The file can not access the file, it is used by another process.

This the only function from where the file is handled and only single instance of this application is running. I also checked the call stack for same function call. How can I handle this problem?
Posted
Updated 18-Apr-11 1:02am
v2

It would be better.
public class WriteIntoFile {
     private static object m_LogSync  = new object();

     public static void Logtext(string txt, string fn){
         lock(m_LogSync){
               using(StreamWriter sw = File.AppendText(fn)){
                     sw.WriteLine(txt);
               }
         }
    }
}


call like this WriteIntoFile.Logtext("abc", "C:\\temp");
I think it would work
 
Share this answer
 
Comments
BobJanova 19-Apr-11 10:11am    
As long as it is only a single process which is using this file (see Nishant's answer about Mutexes – or Mutices? heh – for what to do in the case where it is not), this should work.

Remember that if you are writing a LOT of log information, this approach forces them all to pipeline and blocks other processing on the file I/O, so it may not be appropriate. But unless the log volume is huge that will not be a problem.
If it's an inter-process issue, using lock won't help much since lock is not OS-wide. For inter-process synchronization, you will need to use a Mutex.
 
Share this answer
 
Comments
Naina Dhande 18-Apr-11 7:43am    
How can I implement it!
Naina Dhande 18-Apr-11 7:49am    
Will follwing help?
mtx.WaitOne
followed by code
mtx.RelaeseMutex
Tarun.K.S 18-Apr-11 8:07am    
Just checked in a book. Yes that's right.
if(mutex.WaitOne())
{
try
{
//Work to be done
}
finally
{
mutex.ReleaseMutex();
}
}
else
{
//Some error occured
}
Naina Dhande 19-Apr-11 3:26am    
Use of Mutex is not helping.
Nish Nishant 19-Apr-11 8:35am    
Probably because you are not using it correctly. Share some code.
You could use a FileSystemWatcher object to watch the file to be closed, and then in that event handler, you could do whetever you need to do to the file. Google is your friend, but I wrote an article series about using the FileSystemWatcher object that might help:

FileSystemWatcher - Pure Chaos (Part 1 of 2)[^]

FileSystemWatcher - Pure Chaos (Part 2 of 2)[^]

Part 2 uses the sample app created in Part 1 to illustrate how various programs manipulate files, and how the FileSystemWatcher responds to them.
 
Share this answer
 
You are getting this exception because may be some other process is still writing something to that file.

So you have to first lock the object. Try like that, it may solve your problem.

C#
lock (sw)
{
         sw.WriteLine(txt);
}
 
Share this answer
 
Comments
Naina Dhande 18-Apr-11 7:20am    
Thanks
Tarun.K.S 18-Apr-11 7:23am    
Good answer! My 5. I did learn a new thing here.
Nish Nishant 18-Apr-11 7:28am    
The answer is not accurate though. C#'s lock will not help with inter-process sync-ing.
Tarun.K.S 18-Apr-11 7:59am    
Oh I thought it did work as Naina accepted the answer. Thanks for pointing that out.
Naina Dhande 18-Apr-11 7:44am    
Hi Nitin

sorry! but lock(sw) did not worked

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