2017-03-03 111 views
1

我不知道我在做什麼錯在這裏,但我試圖刪除一個二進制文件,當它沒有運行時,但如果它正在運行,顯示一個消息框告訴用戶在刪除它之前關閉該程序。當我試圖這樣做,它忽略了消息框,並嘗試刪除時,它的運行文件,顯然你不能這樣做,這樣的Visual Studio所以它這個返回:確定一個可執行文件是否正在運行

System.UnauthorizedAccessException的:「訪問路徑'C:\ cmctemp \ lcpol \ lcweb.exe'被拒絕。'

不確定爲什麼它不顯示消息框。

private void button5_Click(object sender, EventArgs e) 
{ 
    Process[] pname = Process.GetProcessesByName("lcweb.exe"); 
    if (pname.Length == 0) 
     if (File.Exists(@"C:\cmctemp\lcpol\lcweb.exe")) 
      File.Delete(@"C:\cmctemp\lcpol\lcweb.exe"); 
     else 
      MessageBox.Show("Please close the program before deleting!", "Information"); 
} 
+1

你可能想'過程。 GetProcessesByName(「lcweb」)' – DavidG

+0

如果你的消息需求是由用戶完成的,你應該首先通過ProcessInfo檢查這個過程,如果你的消息需求是由用戶完成的話 –

+0

正如@DavidG所說,從進程中刪除'.exe'名稱 – Pikoh

回答

5

試着這樣做:

private void button5_Click(object sender, EventArgs e) 
    { 
     Process[] pname = Process.GetProcessesByName("lcweb"); 
     if (pname.Length == 0 && File.Exists(@"C:\cmctemp\lcpol\lcweb.exe")) 
      File.Delete(@"C:\cmctemp\lcpol\lcweb.exe"); 
     else 
      MessageBox.Show("Please close the program before deleting!", "Information"); 
    } 

的第一個變化,我們從刪除名爲 「.exe」 GetProcessByName,第二我只是調整if語句

我希望它能幫助你。

+0

非常感謝!這解決了我的問題。我在c#上還是部分新手。 :) –

+0

埃德尼發現了2個錯誤,好的工作 – Shenron

-2

添加括號來分隔if

private void button5_Click(object sender, EventArgs e) 
{ 
    Process[] pname = Process.GetProcessesByName("lcweb"); 
    if (pname.Length == 0) 
    { 
     if (File.Exists(@"C:\cmctemp\lcpol\lcweb.exe")) 
      File.Delete(@"C:\cmctemp\lcpol\lcweb.exe"); 
    } 
    else 
    { 
     MessageBox.Show("Please close the program before deleting!", "Information"); 
    } 
} 
+3

因爲添加一個否則在嵌套if語句的下面將被視爲File.Exists檢查的其他部分。 – Nathangrad

+1

證據:http://ideone.com/6xFjvq – Nathangrad

+1

當程序運行時,OP想要顯示一個消息框......'if'塊嵌套在他的代碼中,所以如果程序沒有顯示消息框存在,而不是在運行時。 – gobes

0

你的代碼需要2個修復,第一:不要嵌套的if/else沒有括號

private void button5_Click(object sender, EventArgs e) 
    { 
     Process[] pname = Process.GetProcessesByName("lcweb"); 
     if (pname.Length == 0) 
     { 
      if (File.Exists(@"C:\cmctemp\lcpol\lcweb.exe")) 
       File.Delete(@"C:\cmctemp\lcpol\lcweb.exe"); 
     } 
     else 
     { 
      MessageBox.Show("Please close the program before deleting!", "Information"); 
     } 
    } 

你所做的else如果與錯誤鏈接。

對於第二修復,相應地DavidG的回答,Process.GetProcessesByName需要過程的友好名稱而你的情況似乎是「lcweb」

+3

不,OPs代碼中的無支架「if」非常好。 – DavidG

+0

不能:https://dotnetfiddle.net/eQfNiV – Shenron

+0

你的小提琴只是證明我的意見是正確的。 – DavidG

3

你的問題是,lcweb.exe不是有效的過程名稱。從文檔爲Process.GetProcessesByNamehttps://msdn.microsoft.com/en-us/library/z3w4xdc9(v=vs.110).aspx

processName進程的友好名稱。

所以你需要友好的過程名稱。這大概是沒有.exe後綴可執行文件:

Process.GetProcessesByName("lcweb") 

如果你真的需要通過可執行文件的名稱查找的過程中,你需要做這樣的事情:

Process.GetProcesses() 
    .Where(p => p.MainModule.ModuleName == "lcweb.exe") 

然而,你需要確保你的應用程序是64位的,否則你會得到一個異常。

2

首先,你不必檢查文件是否存在:

https://msdn.microsoft.com/en-us/library/system.io.file.delete(v=vs.110).aspx

如果要刪除的文件不存在,不會引發任何異常。

是有很多原因的文件不能刪除,這就是爲什麼我建議 嘗試Delete並在IOException情況下要求用戶

try { 
    File.Delete("lcweb.exe"); 
} 
catch (UnauthorizedAccessException) { 
    // Possible reasons: 
    // 1. The caller does not have the required permission. 
    // 2. The file is an executable file that is in use. <- your case 
    // 3. path is a directory. 
    // 4. path specified a read-only file. 

    // If we are sure that the case "2" can be the only reason 
    MessageBox.Show("Please close the program before deleting!", "Information"); 
} 
+0

但是,使用異常的程序流只是有點髒。 – DavidG

+0

@DavidG:我們無法避免它:'lcweb.exe'可以隨時啓動*(例如,*在檢查後)。 –

+1

@DavidG - 'try/catch'在這裏非常合適。誰關心臟如果它確保程序不會因未處理的異常而崩潰? – Igor

相關問題