0

I have written the c# console application. when i lauch the application (not using cmd). i can see it is listed in process list in task manager ,now i need to write another application in which i need to find whether previous application is running or not ,i know the application name and path so i have written management object searcher query to get the list of process and also i am using the path to compare it with its executable path ,code is given below

            var  name="test.exe"
            var path="D:\test"
            var processCollection = new ManagementObjectSearcher("SELECT * " + "FROM Win32_Process " + "WHERE Name='" + name + "'").Get();

            if (processCollection.Count > 0)
            {
                foreach(var process in processCollection)
                {
                    var executablePath=process["ExecutablePath"];
                    if(executablePath.equals(path))
                    {
                        return true;
                    }
                }
            }

but executable path is always null.

how to get its executable path?.

I can not only use process name because i am using common name like startserver and stopserver to my application. so i need to ensure it executable path.

Mahendran
  • 468
  • 8
  • 19
  • Instead of that tricky process enumeration you should use inter-process communications. E.g. use global mutex (see [this](http://stackoverflow.com/q/93989/1997232)). – Sinatr Jun 29 '15 at 11:31

2 Answers2

0

there is a easier way, import System.Diagnostics then write following code,

public bool ProcessIsRun()
{
    Process[] proc = Process.GetProcesses();
    Return proc.Any(m => m.ProcessName.Contains("Your process name") && m.Modules[0].FileName == "Your File Path" );
}
Behzad
  • 857
  • 1
  • 8
  • 27
-1

This is solution for your problem:

System.Reflection.Assembly.GetEntryAssembly()

This will return complete folder path for executable current application.

You can use System.Reflection.Assembly.GetEntryAssembly().Location to find exact application executable with full path.

Ankush Madankar
  • 3,689
  • 4
  • 40
  • 74
  • can you say why its returning null `process["ExecutablePath"]`. also i want find process executable in another application . – Mahendran Jun 29 '15 at 11:28
  • @Mahendran I dont know what is exactly function of `ManagementObjectSearcher` – Ankush Madankar Jun 29 '15 at 11:31
  • This is not an answer to the question that was asked. `GetEntryAssembly()` will return the assembly for the _current_ process, not some _other_ process as the OP has clearly asked for. – Peter Duniho Sep 22 '17 at 03:11