2016-08-03 4 views
0

전체 디렉터리를 삭제해야하는 프로그램이 있지만 Access to path denied 오류가 발생합니다. 나는 모든 사람에게 모든 것을 제공하지만 도움을받지 못했다.경로 거부 액세스 거부

private void timer1_Tick(object sender, EventArgs e) 
{ 
    label1.Text = DateTime.Now.ToString("HH:mm"); 
    string lowDir = ""; 
    if (label1.Text == comboBox1.Text) 
    { 
     try 
     { 
      if (System.IO.Directory.GetDirectories(txpath1.Text).Length != 0) 
      { 
       try 
       { 
        var folders = Directory.GetDirectories(@"" + txpath1.Text); 
        DateTime lastLow = DateTime.Now; 

        foreach (string subdir in Directory.GetDirectories(@"" + txpath1.Text)) 
        { 
         DirectoryInfo fi1 = new DirectoryInfo(subdir); 
         fi1.Refresh(); 
         DateTime created = fi1.LastWriteTime; 
         if (created < lastLow) 
         { 
          lowDir = subdir; 
          lastLow = created; 
         } 
        } 
        var dir = new DirectoryInfo(@"" + lowDir); 
        Directory.Delete(@"" + lowDir, true); 
       } 
       catch (Exception ex) 
       { 
        this.btStop.PerformClick(); 
        MessageBox.Show(ex.Message+"\nEzért a program leállt."); 
       } 
       finally 
       { 
        timer1.Stop(); 
        timer2.Start(); 
       } 
      } 
      else 
      { 
       this.btStop.PerformClick(); 
       MessageBox.Show("A megadott elérési útvonalon nem találhatóak fájlok a művelet(ek)hez!"); 
      } 
     } 
     catch (DirectoryNotFoundException) 
     { 
      btStop.PerformClick(); 
      MessageBox.Show("Hibás útvonala(kat)t adtál meg!"); 
     } 
    } 
} 
+1

디렉토리를 삭제하기 전에 디렉토리가 비어 있는지 확인 하시겠습니까? 그리고 디렉토리가 읽기 전용이 아닌지 확인하십시오. – Josh

+0

'txpath1.Text'를 정확히 이스케이프하고 있는지 잘 모르겠습니다. – user1666620

+0

Josh, Nope 나는 지금 그것을 확인하지 않았다, 내가 그것을 일하게 할 수 있으면, 나는 할 것이다. 디렉토리가 비어 있지 않습니다. 디렉토리에 mp3, mp4 또는 exe 파일이 포함되어 있으면이 오류가 발생합니다. 만약 내가 그 오류가 발생하지 않는 그 디렉토리에 간단한 PDF 파일을 배치합니다. – celodir

답변

0

재귀 적으로 삭제하려면 Directory.Delete(path, true) 만 사용하면됩니다. 그래도 작동하지 않으면 잘못된 경로가 될 수 있습니다.