How to Get the Full Path of Running Process

How to get the full path of running process?

 using System.Diagnostics;
var process = Process.GetCurrentProcess(); // Or whatever method you are using
string fullPath = process.MainModule.FileName;
//fullPath has the path to exe.

There is one catch with this API, if you are running this code in 32 bit application, you'll not be able to access 64-bit application paths, so you'd have to compile and run you app as 64-bit application (Project Properties → Build → Platform Target → x64).

Find the full path of process

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" );
}

Get full path from a process in Windows

Nothing is wrong with this API, the only thing that's wrong here is your code.

The documentation clearly states that the return value of GetModuleFileNameExW is the length of the string copied to the buffer.

If the return value is 0, the function has failed.

So you simply need to write this:

...
if (GetModuleFileNameExW(hProcess, NULL, filePath, MAX_PATH) != 0)
{
// handle "success" case
}
...

BTW CloseHandle(hProcess); should be inside the if (hProcess != NULL) block.

Full working example with error checks

#include <iostream>
#include <windows.h>
#include <tlhelp32.h>
#include <psapi.h>

int main()
{
HANDLE hSnapProcess = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);

if (hSnapProcess != INVALID_HANDLE_VALUE)
{
PROCESSENTRY32 process;
process.dwSize = sizeof(PROCESSENTRY32);
Process32First(hSnapProcess, &process);
do
{
if (process.th32ProcessID != 0)
{
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, process.th32ProcessID);
if (hProcess != NULL)
{
wchar_t filePath[MAX_PATH];
if (GetModuleFileNameExW(hProcess, NULL, filePath, MAX_PATH))
{
std::wcout << filePath << std::endl;
}
else
{
std::wcout << L"GetModuleFileNameExW failed with error" << GetLastError() << std::endl;
}

CloseHandle(hProcess);
}
}

} while (Process32Next(hSnapProcess, &process));

CloseHandle(hSnapProcess);
}
else
{
std::wcout << L"CreateToolhelp32Snapshot failed with error" << GetLastError() << std::endl;
}
}

C# get the full path of a not yet running Process Object

If you want your UI to be visually-consistent with the rest of the user's machine, you may want to extract the icon from the file using Icon.ExtractAssociatedIcon(string path). This works under the WinForms/GDI world. Alternatively, this question addresses how to complete it with P/Invoke.

Get full path of a running process and save it to a variable

If the executable you're testing for is in your %PATH%, the simplest solution is to use the ~$PATH expansion of a for loop. (In a console window, help for for full details.)

for %%I in (notepad.exe) do set "exepath=%%~$PATH:I"

echo %exepath%

If you need to get the path directly from the process, you can do it with wmic.

wmic process where "name='notepad.exe'" get ExecutablePath

You can capture the result to a variable with a for /f loop. WMI query results are often encoded in a weird encoding (UCS-2 Little Endian, if I recall correctly), so it helps to query a throwaway column to prevent oddness in the capture.

@echo off
setlocal

for /f "tokens=2 delims=," %%I in (
'wmic process where "name='notepad.exe'" get ExecutablePath^,Handle /format:csv ^| find /i "notepad.exe"'
) do set "exepath=%%~I"

echo %exepath%

C++ Windows - How to get process path from its PID

Call OpenProcess to get a handle to the process associated with your PID. Once you have a handle to the process, call GetModuleFileNameEx to get its fully-qualified path. Don't forget to call CloseHandle when you're finished using the process handle.

Here's a sample program that performs the required calls (replace 1234 with your PID):

#include <windows.h>
#include <psapi.h> // For access to GetModuleFileNameEx
#include <tchar.h>

#include <iostream>

using namespace std;

#ifdef _UNICODE
#define tcout wcout
#define tcerr wcerr
#else
#define tcout cout
#define tcerr cerr
#endif

int _tmain(int argc, TCHAR * argv[])
{
HANDLE processHandle = NULL;
TCHAR filename[MAX_PATH];

processHandle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, 1234);
if (processHandle != NULL) {
if (GetModuleFileNameEx(processHandle, NULL, filename, MAX_PATH) == 0) {
tcerr << "Failed to get module filename." << endl;
} else {
tcout << "Module filename is: " << filename << endl;
}
CloseHandle(processHandle);
} else {
tcerr << "Failed to open process." << endl;
}

return 0;
}


Related Topics



Leave a reply



Submit