2

Additional information: The process cannot access the file 'E:\Data.xls' because it is being used by another process.

I am pretty rookie in c# trying to save some data in to an .xls file which gathers the data from five image frames one by one and then saves it in to an .xls file. But i keep running into

An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll Additional information: The process cannot access the file 'E:\Data.xls' because it is being used by another process.

I have found many such questions on the SO but could not resolve the issue. Here is my code:

string path = @"E:\Data.xls";
if (!File.Exists(path))
{
    File.Create(path);
    File.Create(path).Dispose();
}

StringBuilder sb = new StringBuilder();
sb.AppendLine(x + "\t" + y + "\t" + pixel1.R + "\t" + pixel1.G + "\t" + pixel1.B);

sb.AppendLine(x+1 + "\t" + y + "\t" + pixel2.R + "\t" + pixel2.G + "\t" + pixel2.B);

sb.AppendLine(x + "\t" + Convert.ToInt32(y+1) + "\t" + pixel3.R + "\t" + pixel3.G + "\t" + pixel3.B);
sb.AppendLine(x+1 + "\t" +Convert.ToInt32(y+1) + "\t" + pixel4.R + "\t" + pixel4.G + "\t" + pixel4.B);
sb.AppendLine(x-1 + "\t" +Convert.ToInt32(y) + "\t" + pixel5.R + "\t" + pixel5.G + "\t" + pixel5.B);
sb.AppendLine(x + "\t" + Convert.ToInt32(y - 1) + "\t" + pixel6.R + "\t" + pixel6.G + "\t" + pixel6.B);
sb.AppendLine(x-1 + "\t" +Convert.ToInt32(y-1) + "\t" + pixel7.R + "\t" + pixel7.G + "\t" + pixel7.B);
sb.AppendLine(x-1 + "\t" + Convert.ToInt32(y+1) + "\t" + pixel8.R + "\t" + pixel8.G + "\t" + pixel8.B);
sb.AppendLine(x+1 + "\t" + Convert.ToInt32(y-1) + "\t" + pixel9.R + "\t" + pixel9.G + "\t" + pixel9.B);
sb.AppendLine(x+2 + "\t" + y + "\t" + pixel10.R + "\t" + pixel10.G + "\t" + pixel10.B);
sb.AppendLine(x + "\t" + Convert.ToInt32(y+2) + "\t" + pixel11.R + "\t" + pixel11.G + "\t" + pixel11.B);
sb.AppendLine(x+2 + "\t" + Convert.ToInt32(y+2) + "\t" + pixel12.R + "\t" + pixel12.G + "\t" + pixel12.B);
sb.AppendLine(x-2 + "\t" + y + "\t" + pixel13.R + "\t" + pixel13.G + "\t" + pixel13.B);
sb.AppendLine(x + "\t" + Convert.ToInt32(y-2) + "\t" + pixel14.R + "\t" + pixel14.G + "\t" + pixel14.B);
sb.AppendLine(x-2 + "\t" + Convert.ToInt32(y-2) + "\t" + pixel15.R + "\t" + pixel15.G + "\t" + pixel15.B);
sb.AppendLine(x-2 + "\t" + Convert.ToInt32(y+2) + "\t" + pixel16.R + "\t" + pixel16.G + "\t" + pixel16.B);
sb.AppendLine(x+2 + "\t" + Convert.ToInt32(y-2) + "\t" + pixel17.R + "\t" + pixel17.G + "\t" + pixel17.B);
sb.AppendLine(x+2 + "\t" + Convert.ToInt32(y+1) + "\t" + pixel18.R + "\t" + pixel18.G + "\t" + pixel18.B);
sb.AppendLine(x+1 + "\t" + Convert.ToInt32(y+2) + "\t" + pixel19.R + "\t" + pixel19.G + "\t" + pixel19.B);
sb.AppendLine(x+2 + "\t" + Convert.ToInt32(y+2) + "\t" + pixel20.R + "\t" + pixel20.G + "\t" + pixel20.B);
sb.AppendLine(x-2 + "\t" + Convert.ToInt32(y-1) + "\t" + pixel21.R + "\t" + pixel21.G + "\t" + pixel21.B);
sb.AppendLine(x-1 + "\t" + Convert.ToInt32(y-2) + "\t" + pixel22.R + "\t" + pixel22.G + "\t" + pixel22.B);
sb.AppendLine(x+2 + "\t" + Convert.ToInt32(y-1) + "\t" + pixel23.R + "\t" + pixel23.G + "\t" + pixel23.B);
sb.AppendLine(x-1 + "\t" + Convert.ToInt32(y+2) + "\t" + pixel24.R + "\t" + pixel24.G + "\t" + pixel24.B);
sb.AppendLine(x-2 + "\t" + Convert.ToInt32(y+1) + "\t" + pixel25.R + "\t" + pixel25.G + "\t" + pixel25.B);
File.AppendAllText(path, sb.ToString());
sb.Clear();

Moreover I am getting another exception when I am trying to render a webbrowser control like this:

webForm frm = new webForm();
frm.Show();

An unhandled exception of type 'System.Threading.ThreadStateException' occurred in System.Windows.Forms.dll webbrowser control

Any suggestions how can I fix these or what am I doing wrong here would be really appreciated.

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
G droid
  • 956
  • 5
  • 13
  • 36
  • One 'problem' per question. – user2864740 Sep 12 '15 at 07:57
  • @ user2864740 : can you suggest some thong about the first one? – G droid Sep 12 '15 at 08:00
  • 3
    These are basic mishaps. You get the IOException because you are trying to write a file that's already opened by another process. Probably Excel. You'll have to ask the user, nicely, to close it. The ThreadStateException is a programming bug, you are trying to display a web browser in a program that doesn't provide a user interface. A console mode app probably. Use the Windows Forms or WPF project template instead or [this kind of code](http://stackoverflow.com/a/4271581/17034). – Hans Passant Sep 12 '15 at 08:02
  • @Hans Passant: I got it about the web browser control you are right about it....Is there any I can check if the file is is being used by some other process so that i can close it..? – G droid Sep 12 '15 at 08:18
  • Of course, you get the exception. Catch it. You cannot close the process, that's madness. You have to ask *nicely*. – Hans Passant Sep 12 '15 at 08:25

3 Answers3

0

You are opening the file twice, this is causing the IOException.

            if (!File.Exists(path))
            {
                // REMOVE this line: File.Create(path);
               File.Create(path).Dispose();
            }
Richard Schneider
  • 34,944
  • 9
  • 57
  • 73
0

As Richard I'm thinking this

        if (!File.Exists(path))
        {
            // REMOVE this line: File.Create(path);
           File.Create(path).Dispose();
        }

code block is causing the error. So this might solve your Problem:

if (!File.Exists(path))
{
    var file = File.Create(path);
    file.Dispose();
}
cramopy
  • 3,459
  • 6
  • 28
  • 42
0

File.Create creates the file and then returns a stream which you can use to write to the file. to. As long as this stream lives the file is opened.

In your code you have

File.Create(path);
File.Create(path).Dispose();
// snip
File.WriteAllText(..);

Which opens the file three times, but closes it only once.

A better way is to use the using statement to mange the life time of the stream:

using(var fileStream = File.Create(path)) // Using automatically closes the stream when it goes out of scope, the scope is defined by the accolades
{
    // You can then use a stream writer to write to the file stream directly
    using(var streamWriter = new StreamWriter(fileStream))
    {
        streamWriter.WriteLine(x + "\t" + y + "\t" + pixel1.R + "\t" + pixel1.G + "\t" + pixel1.B);
        // Etc..
    }
}

You can do roughly the same thing if a file already exists

if(File.Exist(path))
{
    using(var fileStream = File.OpenWrite(path))
    {
        // same as before
    }
}

Using this technique is a lot less error prone. Of course an IOException still occurs if another program has opened the file.

Roy T.
  • 9,429
  • 2
  • 48
  • 70