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
  • 7,300
  • 10
  • 40
  • 88
masiboo
  • 4,537
  • 9
  • 75
  • 136
  • 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
  • 27,666
  • 26
  • 83
  • 129
0

You could get the path by.

var executionPath = clsProcess.MainModule.FileName
lucky
  • 12,734
  • 4
  • 24
  • 46
  • 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,696
  • 7
  • 43
  • 81
0

You should use ProcessModule.FileName Property:

var runningDir = System.IO.Path.GetDirectoryName(process.MainModule.FileName);
Ali Bahrami
  • 5,935
  • 3
  • 34
  • 53
  • 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
  • 51
  • 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