Possible Duplicate:
Open file ReadOnly
In my application I am writing some information to Log.txt using Debug.WriteLine(), and I want to provide a form which will show the contents of Log.txt.
But when I try to open the file Log.txt I am getting an exception:
The process cannot access the file 'F:\Rajeev\10-11-2012\Temp\Temp\bin\Debug\Log.txt' because it is being used by another process.
How do I overcome this problem?
And here another issue is, I am able to open the same file using Notepad. Then why can't I open the same file using my application?
The following is the code I am using for specifying the log file:
TextWriterTraceListener tr = new TextWriterTraceListener(System.IO.File.CreateText("Log.txt"));
Debug.Listeners.Add(tr);
Debug.AutoFlush = true;
The following is the code I am using for writing to the log file:
Debug.WriteLine("ERROR: Invalid Username " + s);
The following is the code I am using for opening a log file (which is already opened by "Debug") to show in Log Viewer (a "Form" in my application):
File.Open(LogFilePath, FileMode.Open, FileAccess.Read,FileShare.Read);