0

I can find if the target app is currently running or not by this code. But I want to also find out the execution path of that application. But I can't see a way to do it. Please tell me how can I find it execution path?

static public bool IsProcessRunning(string name)
{
    foreach (Process clsProcess in Process.GetProcesses()) 
    {
        if (clsProcess.ProcessName.Contains(name))
        {
            return true;
        }
    }
    return false;
} 
Rand Random
  • 6,633
  • 10
  • 37
  • 81
masiboo
  • 4,117
  • 9
  • 63
  • 122
  • Check out [this](https://stackoverflow.com/questions/5497064/c-how-to-get-the-full-path-of-running-process) thread C#: How to get the full path of running process? – Mallikh Jan 26 '18 at 17:30

5 Answers5

0

You can use following code snippet.

 var process = Process.GetCurrentProcess(); // Or whatever method you are using
 string fullPath = process.MainModule.FileName;

Update

public string GetProcessPath(string name)
{
    Process[] processes = Process.GetProcessesByName(name);

    if (processes.Length > 0)
    {
        return processes[0].MainModule.FileName;
    }
    else
    {
        return string.Empty;
    }
}
santosh singh
  • 26,384
  • 26
  • 81
  • 125
0

You could get the path by.

var executionPath = clsProcess.MainModule.FileName
lucky
  • 12,110
  • 4
  • 20
  • 41
  • Unhandled Exception: System.ComponentModel.Win32Exception: A 32 bit processes cannot access modules of a 64 bit process. – masiboo Jan 26 '18 at 19:43
0

try the MainModule property

clsProcess.MainModule.FileName
Steve
  • 11,116
  • 7
  • 35
  • 72
0

You should use ProcessModule.FileName Property:

var runningDir = System.IO.Path.GetDirectoryName(process.MainModule.FileName);
Ali Bahrami
  • 5,577
  • 3
  • 32
  • 50
  • I get exceptions as: Unhandled Exception: System.ComponentModel.Win32Exception: A 32 bit processes cannot access modules of a 64 bit process. – masiboo Jan 26 '18 at 16:47
0

Process instance and use the MainModule Property to get the location.

Something like this

static public bool IsProcessRunning(string name)
{
   foreach (Process clsProcess in Process.GetProcesses()) 
   {
       if (clsProcess.ProcessName.Contains(name))
          {
            Console.WriteLine(clsProcess.MainModule.FileName);
            return true;
           }
         }
         return false;
} 
Bhavesh Shah
  • 57
  • 1
  • 7
  • Unhandled Exception: System.ComponentModel.Win32Exception: A 32 bit processes cannot access modules of a 64 bit process. – masiboo Jan 26 '18 at 19:44
  • check this out. https://stackoverflow.com/questions/9501771/how-to-avoid-a-win32-exception-when-accessing-process-mainmodule-filename-in-c – Bhavesh Shah Jan 28 '18 at 09:22