2011-08-08 87 views
6

我讀這篇文章(Can't delete a file using threads)關於我的問題,但事情變得越來越困難。使用C#線程刪除文件

我的問題很簡單,我只想刪除這個舊文件,如果我通常啓動方法「dlMoveNovaVersao」文件被刪除,但如果我把它放在一個線程(如波紋管),我得到了「你不是允許」。有人知道有什麼問題? (我想用線程)。

private void verificaVersaoSupervisor_Tick(object sender, EventArgs e) 
    { 
     Thread threadConexao = new Thread(threadVerificaConexao); 
     threadConexao.Start(); 
    } 

    public void threadVerificaConexao() 
    { 
     try 
     { 
      Dns.GetHostEntry("www.google.com.br"); 
      if (verificaVersao()) 
      { 
       try 
       { 
        verificaKillSupervisor(); 
        dlMoveNovaVersao(); 
        Application.Exit(); 
       } 
       catch (Exception) 
       { } 
      } 
      else 
      { 
       Application.Exit(); 
      } 
     } 
     catch (Exception) 
     { } 
    } 

    public void dlMoveNovaVersao() 
    { 
     WebClient webClient = new WebClient(); 
     webClient.DownloadFile("Anywebsite", @"c:\temp\supervisor.exe); 
     try 
     { 
      File.Delete(@"c:\Test\supervisor.exe); //This file is always there! 
     } 
     catch (Exception err) 
     { 
      MessageBox.Show(err.Message); 
     } 

只是discribe的目的,我的程序(主管入門)在網站上查詢,如果我有一箇舊版本的「監督員」的運行(使用XML),如果這是真的我的「超級入門」驗證是否存在稱爲「Supervisor」的進程在「Supervisor Starter」下載新版本並運行後運行並殺死它。 (該程序很小,更新時間不超過4秒)。

當我的「Supervisor Starter」嘗試刪除舊版本的程序時,問題就會出現。如果我使用線程,我收到「我沒有權限訪問該文件」,如果我在Form類上使用相同的方法,則文件被刪除。

+1

人們不同可能經常有這樣的問題,這是一個經典線程問題imho,因此很高興在這裏看到這個級別的問題。 –

回答

4

我懷疑你在使用文件時正在運行線程。線程運行時,它與當前線程並行運行。你確定該文件已關閉嗎?

否則,我認爲該線程可能是使用不屬於您的憑據創建的。但我很確定情況並非如此。

看看這是對每一種情況下

catch (Exception err) 
    { 
     MessageBox.Show("User {0}. Message {1}", 
         System.Security.Principal.WindowsIdentity.GetCurrent().Name, 
         err.Message); 
    } 
+0

同意了,我認爲兩者中的任何一個。不過,我想知道這個問題有多好。 –

+0

Preet,我的文件被關閉了。該系統說:「我沒有權限」 – Carvrodrigo

0

這是我在線程刪除文件的功能,如果該文件正在使用

private static void Delete(System.IO.FileInfo file) 
    { 
     if (file.Exists) 
     { 
      int Attempt = 0; 
      bool ShouldStop = false; 
      while (!ShouldStop) 
      { 
       if (CanDelete(file)) 
       { 
        file.Delete(); 
        ShouldStop = true; 
       } 
       else if (Attempt >= 3) 
       { 
        ShouldStop = true; 
       } 
       else 
       { 
        // wait one sec 
        System.Threading.Thread.Sleep(1000); 
       } 

       Attempt++; 
      } 
     } 
    } 

    private static bool CanDelete(System.IO.FileInfo file) 
    { 
     try 
     { 
      //Just opening the file as open/create 
      using (FileStream fs = new FileStream(file.FullName, FileMode.OpenOrCreate)) 
      { 
       //If required we can check for read/write by using fs.CanRead or fs.CanWrite 
      } 
      return false; 
     } 
     catch (IOException ex) 
     { 
      //check if message is for a File IO 
      string __message = ex.Message.ToString(); 
      if (__message.Contains("The process cannot access the file")) 
       return true; 
      else 
       throw; 
     } 
    } 
+0

Juidan,感謝您的幫助,但該文件沒有使用,這個代碼之前,我充滿了所有的流程和關機「監督員」的數組(它的工作我已經仔細檢查)。 – Carvrodrigo