Joshua Walker
Joshua Walker

Reputation: 13

Determine if an executable is running

I don't know what I'm doing wrong here, but I'm trying to delete a binary file when it's not running, but if it's running, display a message box telling the user to close the program before deleting it. When I tried doing it, it ignores the message box and attempts to delete the file when it's running, obviously you can't do that so Visual Studio so it returns with this:

System.UnauthorizedAccessException: 'Access to the path 'C:\cmctemp\lcpol\lcweb.exe' is denied.'

Not sure why it's not displaying the message box.

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");
}

Upvotes: 1

Views: 597

Answers (5)

Shenron
Shenron

Reputation: 453

Your code needs 2 fixes, first : don't do nested if/else without brackets

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");
        }
    }

You made the else linked with the wrong if.

For the second fix, accordingly to DavidG's answer, Process.GetProcessesByName need the friendly name of the process which in your case seems to be "lcweb"

Upvotes: 0

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186688

First, you don't have to check if the file exists:

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

If the file to be deleted does not exist, no exception is thrown.

There're many reasons why the file can't be deleted, that's why I suggest try to Delete and in case of IOException ask the user

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");
}

Upvotes: 3

DavidG
DavidG

Reputation: 118947

The problem you have is that lcweb.exe is not a valid process name. From the docs for Process.GetProcessesByNamehttps://msdn.microsoft.com/en-us/library/z3w4xdc9(v=vs.110).aspx:

processName: The friendly name of the process.

So you need the friendly name of the process. This is probably be the executable without the .exe suffix:

Process.GetProcessesByName("lcweb")

If you really need to find the process by the name of the executable, you will need to do something like this:

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

However, you will need to ensure your app is 64bit or you will get an exception.

Upvotes: 3

gobes
gobes

Reputation: 562

Add brackets to separate your 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");
    }
}

Upvotes: -2

Edney Batista da Silva
Edney Batista da Silva

Reputation: 376

Try do that:

  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");
  }

The First change we removed ".exe" from GetProcessByName, and the second I just adjust your if statement.

I hope it can help you.

Upvotes: 5

Related Questions