Why the unhandled IOException occur?

There is a CheckBox on my form to let user choose to write data to a file or to another file:

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (cbSave.Checked)
cbSave.Text = "Save to the same file";
else
cbSave.Text = "Save to another file";
}

These are the code to create the path for the file<C:\ExcelRecord\..>

string newDir = @"C:\ExcelRecord";
string path = "";
int fileNo = 1;
if (!Directory.Exists(newDir))
{
Directory.CreateDirectory(newDir);
}
//a new file with the name added "_fileNo" will be created if user want to
if (cbSave.Checked)
path = @"C:\ExcelRecord\" + DateTime.Now.ToString("yyyy-MM-dd") + ".xls";
else
{
DialogResult dlgrst = DialogResult.No;
dlgrst = MessageBox.Show("Do you want to save data to another Excel file?", "Save to Another File", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dlgrst == DialogResult.Yes)
{
path = @"C:\ExcelRecord\" + DateTime.Now.ToString("yyyy-MM-dd") + "_" + fileNo.ToString() + ".xls";
fileNo++;
}

else
{
path = @"C:\ExcelRecord\" + DateTime.Now.ToString("yyyy-MM-dd") + ".xls";
}
}

After checking whether to create a new file:

if (!File.Exists(path))
{
FileStream file = new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.Read);
{
StreamWriter sw = new StreamWriter(file);
sw.WriteLine(String.Format("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}\t{8}\t{9}\t{10}\t{11}\t{12}", "Time", "Channel 0", "Channel 1", "Channel 2", "Channel 3", "Channel 4", "Channel 5", "Channel 6", "Channel 7", "Channel A", "Channel B", "Channel C", "Channel D"));
sw.Flush();
sw.Close();
}
}

//write data from the COM Port to the excel file
using (StreamWriter sw = new StreamWriter(path,true))
{
........................................
........................................
}

However, when I run my application, errors will be shown:
The process cannot access the file 'C:\ExcelRecord\2006-09-26.xls' because it is being used by another process.

I do not know why....I did not open the Excel file.

Any help?

Thanks!

[2992 byte] By [Pockey] at [2007-12-24]
# 1

even though you should not need to as when closing a stream, it closes its basestreams etc... - check to see if after closing the StreamWriter, if the FileStream is not null, if not, then close it. Or even better try this:

using (FileStream file = new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.Read))
{
using (StreamWriter sw = new StreamWriter(file))

{
sw.WriteLine(String.Format("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}\t{8}\t{9}\t{10}\t{11}\t{12}", "Time", "Channel 0", "Channel 1", "Channel 2", "Channel 3", "Channel 4", "Channel 5", "Channel 6", "Channel 7", "Channel A", "Channel B", "Channel C", "Channel D"));
}

}

It could be because the stream writer did not close the FileStream at all and next time you try to open it, it can't open it because its "locked" by the FileStream. Also make sure that you did not open the file in Excel after writing to it - sometimes it will fail and cause this error

does this work for you?


ahmedilyas at 2007-8-31 > top of Msdn Tech,Visual C#,Visual C# General...
# 2

Thanks ahmedilyas!

Well, I try to write to the file twice because I will call the method whenever there is data ready for writing..

With you code, I will have many rows of "Time, Channel 0 ...." in the Excel file.

I want to first check whether the file exits or not. If yes, no need to write the first row of "Time..." to it, if not, I should first write the first row "Time, Channel 0 ..." to it as the header...

I will try anyway...

Pockey at 2007-8-31 > top of Msdn Tech,Visual C#,Visual C# General...
# 3
the whole rows issue will be to do with your code on the way you are writing to the excel file - the current issue stands as you had stated, with the IOException and I believe that somewhere, either the StreamWriter or FileStream, is not closing the file properly so next time when you try to write - it can't as the file is in use :-)
ahmedilyas at 2007-8-31 > top of Msdn Tech,Visual C#,Visual C# General...
# 4

There will be no problems if I did not add the function for saving to another file...

The program runs OK for the following codes:

string newDir = @"C:\ExcelRecord";
string path = "";
if (!Directory.Exists(newDir))
{
Directory.CreateDirectory(newDir);
}
path = @"C:\ExcelRecord\" + DateTime.Now.ToString("yyyy-MM-dd") + ".xls";

if (!File.Exists(path))
{
FileStream file = new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.Read);
{
StreamWriter sw = new StreamWriter(file);
sw.WriteLine(String.Format("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}\t{8}\t{9}\t{10}\t{11}\t{12}", "Time", "Channel 0", "Channel 1", "Channel 2", "Channel 3", "Channel 4", "Channel 5", "Channel 6", "Channel 7", "Channel A", "Channel B", "Channel C", "Channel D"));
sw.Flush();
sw.Close();
}
}

//write data from the COM Port to the excel file
using (StreamWriter sw = new StreamWriter(path,true))
{
........................................
........................................
}

So the error should be related to the codes to check which file user wants to write to ....
However, I can not find out the point...

Pockey at 2007-8-31 > top of Msdn Tech,Visual C#,Visual C# General...