Check if a specific exe file is running

后端 未结 8 1907
野趣味
野趣味 2020-12-01 04:44

I want to know how i can check a program in a specific location if it is running. For example there are two locations for test.exe in c:\\loc1\\test.exe and c:\\loc2\\test.e

相关标签:
8条回答
  • 2020-12-01 05:08
    System.Reflection.Assembly.GetEntryAssembly()
    

    This will bring for you a lot of info about the entry assembly, such as:

    System.Reflection.Assembly.GetEntryAssembly().CodeBase;
    

    This will tell the location of the running assembly.

    0 讨论(0)
  • 2020-12-01 05:11

    You should iterate over all existing processes and then check their MainModule property for the file name you are looking for. Something like this

    using System.Diagnostics;
    using System.IO;
    
    //...
    
    string fileNameToFilter = Path.GetFullPath("c:\\loc1\\test.exe");
    
    foreach (Process p in Process.GetProcesses())
    {
       string fileName = Path.GetFullPath(p.MainModule.FileName);
    
       //cehck for equality (case insensitive)
       if (string.Compare(fileNameToFilter, fileName, true) == 0)
       {
          //matching...
       }
    }
    
    0 讨论(0)
提交回复
热议问题