2

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

T.S.
  • 18,195
  • 11
  • 58
  • 78
Gaurav Mody
  • 79
  • 1
  • 9
  • See [here](http://stackoverflow.com/questions/11961137/getting-a-path-of-a-running-process-by-name) or [here](http://stackoverflow.com/questions/5497064/c-how-to-get-the-full-path-of-running-process). – Kenney Feb 08 '16 at 19:14
  • @Kenney ..Thanks for that but in my case path wont help..anything other than path might be a good help. – Gaurav Mody Feb 08 '16 at 19:22

3 Answers3

1

Use their process Id.

private static void Main(string[] args)
{
    var procs = Process.GetProcesses();
    foreach ( var proc in procs )
    {
        Console.WriteLine(proc.Id);
    }
}
Brandon
  • 4,491
  • 6
  • 38
  • 59
1

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

0

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 "";
    }
}
ascriven
  • 362
  • 3
  • 6