我收到错误:
该进程无法访问文件'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++;
}
}
}
答案 0 :(得分:0)
如果这种情况
if (dt[playback_iterator].iden == this.event_id)
不正确,StreamWriter
未确定关闭,下次尝试打开文件时,您可能会遇到访问冲突。
使用using
声明
using (StreamWriter sw1 = new StreamWriter("E:\\testing\\check1.txt"))
{
}
然后你也可以删除sw1.Close()
并且作者总是关闭一次。
注意:错误也可能意味着当前进程使用该文件。我不认为记事本保持文件打开,所以记事本不会有问题。
答案 1 :(得分:0)
不要关闭StreamWriter对象,并且如果您尝试追加此错误..使用FileStream类在每次在循环内创建时附加日志
string logFile = "E:\\testing\\check1.txt";
FileStream fs = new FileStream(logFile, FileMode.Append, FileAccess.Write);
StreamWriter sw1 = new StreamWriter(fs);
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");
}
}
}
}
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++;
}
}
sw1.Close();
fs.Close();
由于
答案 2 :(得分:0)
您可以使用using
阻止并写入文本文件
using (StreamWriter sw1 = new StreamWriter("E:\\testing\\check1.txt"))
{
if (condition)
{
foreach (Type type in asm1.GetTypes())
{
if (contition)
{
sw1.WriteLine("verification point was set and the test passed");
}
}
}
}