【发布时间】:2012-05-02 06:37:19
【问题描述】:
我收到一个错误:
进程无法访问文件“E:\testing\check1.txt”,因为它 正在被另一个进程使用。
这是我的代码:
private void timer2_Tick(object sender, EventArgs e)
{
StreamWriter sw1 = new StreamWriter("E:\\testing\\check1.txt");
sw1.Flush();
if (dt[playback_iterator].iden == this.event_id)
{
foreach (Type type in asm1.GetTypes())
{
if (type.IsSubclassOf(typeof(System.Windows.Forms.Form)))
{
System.Windows.Forms.Form f = (System.Windows.Forms.Form)Activator.CreateInstance(type);
foreach (Control ctrl in f.Controls)
{
if (ctrl.Handle.ToInt32() == dt[playback_iterator].hndl)
{
if (ctrl.BackColor.R == this.r_comp && ctrl.BackColor.G == this.g_comp && ctrl.BackColor.G == this.g_comp)
{
sw1.WriteLine("verification point was set and the test passed");
/*success ob = new success();
ob.Show();*/
}
else
{
sw1.WriteLine("verification point test failed");
}
}
}
}
sw1.Close();
if (dt[playback_iterator].hndl == -1 && dt[playback_iterator].x == -1 && dt[playback_iterator].y == -1)
{
timer2.Enabled = false;
}
MoveMouse(dt[playback_iterator].hndl, dt[playback_iterator].x, dt[playback_iterator].y);
if (dt[playback_iterator].click_detect.Equals("yes"))
{
ClickMouse(MonkeyButtons.btcLeft, dt[playback_iterator].x, dt[playback_iterator].y, 0, 0);
}
if (dt[playback_iterator].word != "")
{
++count;
StringBuilder wd = new StringBuilder(dt[playback_iterator].word);
SetForegroundWindow(dt[playback_iterator].hndl);
SendKeys.Send(dt[playback_iterator].word);
}
playback_iterator++;
}
}
}
【问题讨论】:
-
错误说明了您需要知道的内容。它已被另一个进程使用。您可能会在记事本或其他任何窗口中打开它,或者其他应用程序已经在使用该文件,就像您尝试的那样。
-
不知道,但似乎您正在 foreach() 循环中关闭 File 处理程序,这意味着在第二次迭代等时,任何写入尝试显然都会失败.
标签: c# automation