Click here to Skip to main content
15,915,501 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C#
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();
}
Posted
Updated 28-Oct-10 9:39am
v3

That can happen when there are pending writes. Make sure you call FileStream.Flush after you write to the file, and you should not have this problem.

Even if you share the file for read access, during a write, the file has to be partially locked (for obvious reasons).
 
Share this answer
 
v2
Comments
aidin Tajadod 28-Oct-10 15:41pm    
Thanks Nishant,
I call FileStream.Flush after each writing.I have read some where that it is a bug in .net !!
I have updated my question and added my code.
The problem is still exist! :(
Nish Nishant 28-Oct-10 15:46pm    
Considering that Notepad can open the file, I don't think there's a bug in .NET's implementation of FileShare.Read. If at all there's a problem, it seems to be with Excel.

Where did you read that there's a bug in .NET that results in this behavior?
aidin Tajadod 28-Oct-10 15:54pm    
I don't know exactly. I was googling for this problem for a few days and read that.
aidin Tajadod 28-Oct-10 15:56pm    
One thing else, both notepad and excel open the file. but when I open the file with Excel I get that error in my Application not in Excel.
Nish Nishant 28-Oct-10 16:01pm    
Now you have FileAccess.Write. Even if you don't read back, can you try changing that to FileAccess.ReadWrite? I want to test a hunch.
Change the 3rd parameter to FileAccess.ReadWrite.

Change the last parameter to System.IO.FileShare.None. Once you open the file with your app, no other file can open the file.
 
Share this answer
 
Comments
aidin Tajadod 28-Oct-10 14:00pm    
Thanks,but I need it to be opened for reading by other Applications!
It seems there is no problem if I remove flush!! but I do not know whether it good practive or not!

Edit1:
It actually did not solve the problem. It just happens less now!!
 
Share this answer
 
v3

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