我的程序是先生成一个文件,然后获取这个文件的地址,然后将文件读入textbox中。但生成文件需要时间,所以要加个等待和判断,但我加了后程序总报错,说“文件D:\\PinGUI2\\PinGUI\\bin\\Debug\\imageload.out正被另一个进程使用,无法访问”,还请大神们帮我看看程序错在哪,我不太会用进程。 //读入文件到标签页中
if (File.Exists(@"D:\\PinGUI2\\PinGUI\\bin\\Debug\\imageload.out"))//文件是否存在,是
{
StreamReader reader = new StreamReader(@"D:\\PinGUI2\\PinGUI\\bin\\Debug\\imageload.out");
textBox2.Text = reader.ReadToEnd();
}
else
{
System.Threading.Thread.Sleep(5000);//等待五秒
StreamReader reader = new StreamReader(@"D:\\PinGUI2\\PinGUI\\bin\\Debug\\imageload.out");
textBox2.Text = reader.ReadToEnd();
}
if (File.Exists(@"D:\\PinGUI2\\PinGUI\\bin\\Debug\\imageload.out"))//文件是否存在,是
{
StreamReader reader = new StreamReader(@"D:\\PinGUI2\\PinGUI\\bin\\Debug\\imageload.out");
textBox2.Text = reader.ReadToEnd();
}
else
{
System.Threading.Thread.Sleep(5000);//等待五秒
StreamReader reader = new StreamReader(@"D:\\PinGUI2\\PinGUI\\bin\\Debug\\imageload.out");
textBox2.Text = reader.ReadToEnd();
}
这是我生成文件的代码,我看了一早上也没看出问题啊谢大神 Process p = new Process();
p.StartInfo.FileName = "cmd.exe"; //设置启动的进程命令,启动cmd命令提示符
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardInput = true; //重新定向输入
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = true; //是否启动新的窗口,否
p.Start();
p.StandardInput.WriteLine("d:");
p.StandardInput.WriteLine("cd pin");
p.StandardInput.WriteLine("pin -t d:\\pin\\source\\tools\\ManualExamples\\obj-ia32\\imageload.dll -- " + fileselecpath.Text);//转义字符"+"
其实可以这样写@"D:\PinGUI2\PinGUI\bin\Debug\imageload.out"
文件流操作完必须要close,要不然再操作的话,会报错的