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.)