Hi guys,
I am opening a text file (.csv) with the below command:
_FileStream = System.IO.File.Open(_CSVFileName, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write, System.IO.FileShare.Read);
and I do not close the file while my program is runing.
when I open a file in my program and after that when I open the same file with Excel ( althogh Excel ask me to open the file as readonly ) sometimes I get the following Error:
The process cannot access the file because another process has locked a portion of the file.
It seems that .net does not lock the file completely !! (It works fine when I open the file with notepad)
Is there any solution?!
Thanks.
Edited:
I am just writing in the file. so I open the file with FileAccess.Write. but I want other users can read the file when it is open by my App.
( It is my Application log file)
Edited 2:
This is my Code:
try
{
oMutex.WaitOne();
byte[] obyte;
obyte = System.Text.Encoding.ASCII.GetBytes(str);
_FileStream.Write(obyte, 0, str.Length);
_FileStream.WriteByte(13);
_FileStream.WriteByte(10);
_Line++;
_FileStream.Flush();
obyte = null;
}
finally
{
oMutex.ReleaseMutex();
}