2012-05-02 4 views
2

나는 오류가 점점 오전 :이 조건'다른 프로세스에서 파일을 사용 중이므로 프로세스가 파일에 액세스 할 수 없습니다.'오류가 발생합니다.

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

사실이 아닌 경우

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++; 

     } 
    } 
} 
+3

닫기 notepad :) – Reniuz

+0

오류는 당신이 알아야 할 것을 말합니다. 이미 다른 프로세스에서 사용됩니다. 메모장의 창에서 파일을 열어 보거나 다른 응용 프로그램에서 파일을 이미 사용하고있는 것처럼 할 수도 있습니다. – SimpleVar

+4

모르겠지만 foreach() 루프 내에서 파일 핸들러를 닫는 것처럼 보이므로 두 번째 반복에서 모든 쓰기 시도가 실패하게됩니다. – Shai

답변

0

StreamWriter는 결정적으로 폐쇄되지 않고 다음 시간 : 여기

The process cannot access the file 'E:\testing\check1.txt' because it is being used by another process.

은 내 코드입니다 액세스 위반 가능성이있는 파일을 열려고 시도합니다.

는 당신은 또한 sw1.Close()을 제거 할 수있는 using

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

을 사용하고 작가는 항상 정확히 한 번만 닫힙니다.

참고 : 오류는 현재 프로세스에서 파일을 사용하고 있음을 의미 할 수도 있습니다. 메모장이 파일을 열어 둔다고 생각하지 않아 메모장이 문제가되지 않을 것입니다.

0

는 StreamWriter는 개체를 닫고,이 오류를 추가하려는 경우 .. 로그를 APPEND 당신은 루프 내에서

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(); 

감사

0

당신이 할 수를 만들 때마다하여 FileStream 클래스를 사용하지 마십시오 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"); 
      } 
     } 
    } 

}

관련 문제