2

我收到一个错误:

该进程无法访问文件“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++;

        }
    }
}
4

3 回答 3

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");
            }
        }
    }

}

于 2012-05-02T06:53:58.717 回答
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();

谢谢

于 2012-05-02T06:53:09.400 回答
0

如果这个条件

if (dt[playback_iterator].iden == this.event_id)

不是真的,StreamWriter没有确定性地关闭,下次您尝试打开文件时,您可能会遇到访问冲突。

使用using声明

using (StreamWriter sw1 = new StreamWriter("E:\\testing\\check1.txt"))
{
}

那么你也可以删除sw1.Close()并且作者总是只关闭一次。

注意:错误也可能意味着文件被当前进程使用。我认为记事本不会使文件保持打开状态,因此记事本不会有问题。

于 2012-05-02T06:50:05.583 回答