开发者

The process cannot access the file

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


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.


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜