gauravmody February 2016

How to distinguish multiple processes with same name run by totally different Windows forms applications?

I have two applications with different functionalities. One of which is an existing application and the other one made by me purposefully with the same name.

When they are started by the user, process name is same for both.

Using c#, VS 2010, .Net 2.0 I want to distinguish between the two and them when I get the process list using c#.

Target OS : XP / Win 7 / Win 8 / Win 10

Answers


Brandon February 2016

Use their process Id.

private static void Main(string[] args)
{
    var procs = Process.GetProcesses();
    foreach ( var proc in procs )
    {
        Console.WriteLine(proc.Id);
    }
}


Rÿck Nöthing February 2016

You could use the MainModule property of each process to distinguish them by version info: https://msdn.microsoft.com/en-us/library/system.diagnostics.process.mainmodule%28v=vs.110%29.aspx


ascriven February 2016

If you can find the process file location you can determine which process it is. This is a little console application that prints out each process path. Just change the "MyProcessName" to your process name. You have to add reference to System.Management and include System.Diagnostics and System.Management.

I found the MainModule property not to work very well because only 32 bit can read 32 bit programs and vice versa for 64 bit programs

class Program
{
    static void Main(string[] args)
    {
        var Processes = Process.GetProcesses();
        foreach(var proc in Processes)
        {
            if(proc.ProcessName=="MyProcessName")
            Console.WriteLine(ProcessExecutablePath(proc));
        }
        Console.ReadKey();
    }
    static private string ProcessExecutablePath(Process process)
    {
        try
        {
            return process.MainModule.FileName;
        }
        catch
        {
            string query = "SELECT ExecutablePath, ProcessID FROM Win32_Process";
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);

            foreach (ManagementObject item in searcher.Get())
            {
                object id = item["ProcessID"];
                object path = item["ExecutablePath"];

                if (path != null && id.ToString() == process.Id.ToString())
                {
                    return path.ToString();
                }
            }
        }

        return "";
    }
}

Post Status

Asked in February 2016
Viewed 1,679 times
Voted 6
Answered 3 times

Search




Leave an answer