[英]An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll The directory name is invalid
[英]why does “An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll (locked by some .exe)” Occur?
附加信息:该进程无法访问文件'E:\\ Data.xls',因为它正在被另一个进程使用。
我是C#中的新秀,试图将一些数据保存到.xls文件中,该文件从五个图像帧中一个接一个地收集数据,然后将其保存到.xls文件中。 但是我一直在遇到
mscorlib.dll中发生了类型为'System.IO.IOException'的未处理异常。其他信息:该进程无法访问文件'E:\\ Data.xls',因为它正在被另一个进程使用。
我在SO上发现了许多此类问题,但无法解决问题。 这是我的代码:
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();
此外,当我尝试呈现如下所示的Web浏览器控件时,我遇到另一个异常:
webForm frm = new webForm();
frm.Show();
System.Windows.Forms.dll Webbrowser控件中发生了类型为'System.Threading.ThreadStateException'的未处理异常
我将如何解决这些问题或在这里做错了什么建议。
您打开文件两次,这导致IOException
。
if (!File.Exists(path))
{
// REMOVE this line: File.Create(path);
File.Create(path).Dispose();
}
作为理查德,我在想这个
if (!File.Exists(path))
{
// REMOVE this line: File.Create(path);
File.Create(path).Dispose();
}
代码块导致错误。 因此,这可能会解决您的问题:
if (!File.Exists(path))
{
var file = File.Create(path);
file.Dispose();
}
File.Create
创建文件,然后返回可用于写入文件的流。 至。 只要该流存在,就将打开文件。
在您的代码中
File.Create(path);
File.Create(path).Dispose();
// snip
File.WriteAllText(..);
它将打开文件三次,但仅关闭一次。
更好的方法是使用using
语句来管理流的生命周期:
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..
}
}
如果文件已经存在,您可以做大致相同的事情
if(File.Exist(path))
{
using(var fileStream = File.OpenWrite(path))
{
// same as before
}
}
使用这种技术可以减少很多错误。 当然,如果另一个程序打开了文件,仍然会发生IOException
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.