Pattern: Use of Process.GetCurrentProcess().MainModule.FileName
Issue: -
System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName
is expensive:
- It allocates a
Process
andProcessModule
instance, usually just to get theFileName
. - The
Process
instance needs to be disposed, which has a performance impact. - It's easy to forget to call
Dispose()
on theProcess
instance. - If nothing else besides
FileName
uses theProcess
instance, then the linked size grows unnecessarily by increasing the graph of types referenced. - It is somewhat difficult to discover or find this API.
System.Environment.ProcessPath
avoids all of these downsides and produces the same information.
Example of incorrect code:
string path = Process.GetCurrentProcess().MainModule.FileName;
Example of correct code:
string path = System.Environment.ProcessPath;