【发布时间】:2015-09-12 16:48:06
【问题描述】:
附加信息:该进程无法访问文件“E:\Data.xls”,因为它正被另一个进程使用。
我是 C# 的新手,试图将一些数据保存到 .xls 文件中,该文件从五个图像帧中一一收集数据,然后将其保存到 .xls 文件中。但我一直遇到
“System.IO.IOException”类型的未处理异常发生在 mscorlib.dll 附加信息:进程无法访问 文件 '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();
此外,当我尝试呈现这样的网络浏览器控件时,我遇到了另一个异常:
webForm frm = new webForm();
frm.Show();
“System.Threading.ThreadStateException”类型的未处理异常 发生在 System.Windows.Forms.dll webbrowser 控件中
任何建议我该如何解决这些问题或我在这里做错了什么将不胜感激。
【问题讨论】:
-
每个问题一个“问题”。
-
@user2864740 : 你能推荐一些关于第一个的丁字裤吗?
-
这些是基本的事故。您收到 IOException 是因为您正在尝试编写已被另一个进程打开的文件。应该是Excel。您必须很好地要求用户关闭它。 ThreadStateException 是一个编程错误,您试图在不提供用户界面的程序中显示 Web 浏览器。可能是控制台模式应用程序。请改用 Windows 窗体或 WPF 项目模板或 this kind of code。
-
@Hans Passant:我知道关于 Web 浏览器控件的信息,你是对的......有什么我可以检查文件是否正在被其他进程使用以便我可以关闭它..?
-
当然,你得到了例外。抓住它。你不能关闭这个过程,那太疯狂了。你必须很好地问。
标签: c# multithreading excel