Click here to Skip to main content
15,915,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys,

Please look at the code below:

System.IO.FileStream oFS = new System.IO.FileStream("c:\\tt.csv",System.IO.FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.Read);
int i = 0;
while (true)
{
    string str;
    byte[] oByte;
    str="5886A," + "Program,"+ "Main,"+ "This is a Test :" + i.ToString();
    oByte = System.Text.Encoding.ASCII.GetBytes(str);
    oFS.Write(oByte, 0, oByte.Length);
    oFS.Write(new byte[]{13,10}, 0, 2);
    oFS.Flush();
    i++;
    str = "5886A," + "Program," + "Main," + "This is a Test :" + i.ToString();
    oByte = System.Text.Encoding.ASCII.GetBytes(str);
    oFS.Write(oByte, 0, oByte.Length);
    oFS.Write(new byte[] { 13, 10 }, 0, 2);
    oFS.Flush();
    i++;
    str = "5886A," + "Program," + "Main," + "This is a Test :" + i.ToString();
    oByte = System.Text.Encoding.ASCII.GetBytes(str);
    oFS.Write(oByte, 0, oByte.Length);
    oFS.Write(new byte[] { 13, 10 }, 0, 2);
    oFS.Flush();
    i++;
    str = "5886A," + "Program," + "Main," + "This is a Test :" + i.ToString();
    oByte = System.Text.Encoding.ASCII.GetBytes(str);
    oFS.Write(oByte, 0, oByte.Length);
    oFS.Write(new byte[] { 13, 10 }, 0, 2);
    oFS.Flush();
    System.Threading.Thread.Sleep(10);
    if (i > 56000) break;
}
oFS.Close();


This code simply create or open a csv (text) file and start writing in it.
If I open the csv file with Microsoft Excel (2003 or 2007) while the program is writing in this file, most of the times I get this Error:

The process cannot access the file because another process has locked a portion of the file.

Note:
Microsoft Excel ask to open the file as readonly. and when I confirm that, I get the Error.
( I have not tested this with Excel 2010)

Can anyone please tell me what is going on?!!!

Thanks.

(I had asked this question before, but now I simplified my question with a very simple source code.)
Posted
Comments
Toli Cuturicu 2-Nov-10 14:31pm    
Don't put the file in the root directory!
aidin Tajadod 2-Nov-10 14:44pm    
I did it just for test. In real it is not.

1 solution

When you write to the file, the OS locks it. This prevents other applications from accessing it and causing problems. I hacked this code to test:

C++
FileStream fs = new FileStream(@"c:\foobar.txt", FileMode.Append, FileAccess.Write);
while (true)
{
    fs.WriteByte((byte)'*');
    fs.Flush();
}


I opened the file in text pad several times without incident while the file was being written.
Excel seems to work in a pretty strange way:

  1. Open file: Excel reports the file is already locked. The code then throws and error as Excel has locked it while it isn't being written. Excel must repeatedly try to lock the file.
  2. Open as Readonly: Excel does not display the error popup, but it does lock when it isn't being written the file as the code throws the next time it tries to write


I have no idea why Excel needs to lock the file in readonly mode, but it does, so there it is. I doubt there is much you can do about this, other than re-design or work around the problem.


You can verify that you application has the file open using the unlocking tool available here:

http://ccollomb.free.fr/unlocker/[^] it lists the process locking the file.
 
Share this answer
 
v2
Comments
Nish Nishant 2-Nov-10 16:07pm    
I don't think that explains this. Excel cannot get a write-lock on the file because when Excel opens it, the file is already open with write-access in the other process.
aidin Tajadod 2-Nov-10 16:59pm    
Thanks Keith,
So What FileShare.Read means? If so there is no meaning if you specify FileShare.Read Flag!!
Nish Nishant 2-Nov-10 21:34pm    
Btw aiden, I notice that Keith got a 2 vote. If that's you, I have a request. If a post helped you, vote it 4 or 5. If you think it didn't help you, but that the person who replied was trying to help you, don't down-vote. Every time you down-vote a post (1 to 3), he will lose rep points. I am sure Keith does not care about his rep points but it's a little sad that he has to lose points for trying to help you. Of course if you did not cast that vote, please ignore this comment.
aidin Tajadod 2-Nov-10 23:49pm    
Hi Nishant, It was not me, but thanks to tell me. I did not know that:)
Keith Barrow 3-Nov-10 3:54am    
@nishant, I mis-read the question, I'll change my answer!

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