How do I get the path of a binary in c#? -


for reporting purposes i'm trying location of binary.

i doing this, , working, i'm getting nullreferenceexception when try testproc.mainmodule.filename; , think may program closing before i'm able grab it. there better way this?

processstartinfo testpsi = new processstartinfo(runoptions.testbinary); testpsi.redirectstandarderror = true; testpsi.redirectstandardoutput = true; testpsi.useshellexecute = false; process testproc = new process(); testproc.startinfo = testpsi;    testproc.start(); ret = testproc.mainmodule.filename; testproc.kill(); if (ret != null)     return ret; 

option 1: use path have.

unless i'm missing something, looks you're creating process (with runoptions.testbinary), should know path. can full path executable path.getfullpath(runoptions.testbinary), or if binary on path, can manually examine each directory determine binary being executed from.

option 2: use c#'s wmi libraries:

if there differences in bitness (32-bit/64-bit) between test host process , process being executed, enumerating modules , getting file name not work. can use wmi around limitation, however.

add reference system.management , try following (warning: no error handling):

static string getimagepath(int processid) {     string query = string.format("select executablepath win32_process processid='{0}'", processid);     managementobjectsearcher searcher = new managementobjectsearcher(query);     managementobjectcollection results = searcher.get();     managementobject process = results.cast<managementobject>().first();     return (string)process["executablepath"]; } 

option 3: add synchronization target process:

if timing issue (i.e. monitored process dying before test process can examine it), can add synchronization (e.g. named mutex) between 2 processes. if want dig deep, can use etw capture process start events (see below).

option 4: use the traceevent .net library capture process start events.

vance morrison (who wrote traceevent library) has an in-depth article on how capture process start events. solution require pulling in bunch of dependencies, since uses etw, there not timing issue (even if process dies quickly, process start event still fired). far complex solution.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -