1

I have a program that I use to write data to an excel file.... and at the same time I use an excel file to import data from the first excel file using (import data) tool.

This is part of the program code :

private void Log(string pText)
{
    //textBox1.AppendText(pText + "\r\n"); // does not put cr lf, why?
    this.BeginInvoke(new MethodInvoker(delegate () {
        textBox1.AppendText(pText);
        textBox1.AppendText("\n");
    }));
}

private void LogStatus(string pText)
{
    textBox1.AppendText(pText + "\t");
    textBox1.AppendText("\n");
}


StreamWriter theString = new StreamWriter(FileName + ".csv", true);
theString.WriteLine(DateTime.Now + dataToSave + "\t\n");
theString.Close();

my problem is when i start to debuge my program in Visual C# 2008

it will work will and start to write data to the first excel file....then during the debug process

i want to import data from the first excel file to the second one using import data(it's important for me to do that in real time i cant delay the import process)

the debuging process stopped and the visual C# 2008 higlight this line of the code


 theString.Close();

and a message popup to me said

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

any help in overcome this problem plz

and thanks

yanckst
  • 438
  • 4
  • 17
  • 1
    It looks as if the problem is with creating the *first* file. Do you properly close the file then? – Dirk Vollmar Sep 09 '10 at 08:34
  • 1
    Does Excel still have the file open? If so, that is the problem. – leppie Sep 09 '10 at 08:34
  • i dont open the first excel file..i use another one to import data form the first (importing is a must for me) i cant delay importing cos. i have many data that the program writ in real time feed and i want to veiw that data using import function (also in real time) – user14825666 Sep 09 '10 at 08:35

3 Answers3

0

Please provide us with the code that opens the first file also, so we can see the big picture here.

Obviously there is somewhere that this file is locked, and that might be the case even if you manage to get read and write access to that file yourself using the StreamReader.

So your problem is not with this code, but the code that happens to lock the file.

The file might also be locked by another application of course, but if this is a file that is only used by your program, then it should not be a problem.

Do you use any Excel interop to read the file in the first place? Could it be that you are not properly closing the file from there, and that Excel still have this file open? In that case you can probably check if a Excel.exe process is running on your machine when you encoutner this error, even if Excel looks like it's not running.

Øyvind Bråthen
  • 59,338
  • 27
  • 124
  • 151
  • hi iv just add the other part of the code that the program use to write to the excel file – user14825666 Sep 09 '10 at 09:05
  • I see that you use BeginInvoke, that causes the delegate you provide to run asyncronously. Since you only have given part of your code here, I can only guess, but I think that your async code that reads the file might not have finished running and therefore have a lock on this file. – Øyvind Bråthen Sep 09 '10 at 09:31
0

You can check for the related permissions before rwx. This answer might help.

Community
  • 1
  • 1
KMån
  • 9,896
  • 2
  • 31
  • 41
0

Maybe you've got an open stream or sth. close them and retry.