2017-03-03 227 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

相关问题