如何使用C#获取进程执行路径 [英] How to get processe execution path using C#
问题描述
嗨亲爱的
我开发了一个喜欢windows TaskManager的流程管理器。现在我想得到进程执行路径。
我使用了Process.MainMadule.FileName但是程序抛出异常。
只是ReadProcessMemory或WriteProcessMemory的一部分请求已完成
我的程序平台目标是AnyCPU,我使用.Net 2.0。此外,我使用GetModuleFileNameEx API来获取进程位置,但有时它返回null或Access是Denied Exception(注意我的程序以管理员身份运行)。
我的代码在这里:
var findCurrentProcessById = new Hashtable();
foreach (Process proc in Process.GetProcesses())
{
if (proc.Id == 0 || proc.Id == 4 || proc.ProcessName.ToLower()== audiodg .ToLower())
继续;
string pid;
尝试
{
pid = proc.Id + < span class =code-string> $ + proc.StartTime.Ticks;
findCurrentProcessById.Add(pid,proc);
}
catch (例外){继续; }
if (findProcessById.ContainsKey(pid))
continue ;
// 流程已开始!
var plt = new ProcessLifeTime()
{
PID = proc.Id,
流程= proc,
ProcessName = proc.ProcessName,
StartTime = proc.StartTime,
};
尝试
{
/ * 在这一行中有时会抛出异常。或者当我使用WMI时路径为空或有时抛出Access is Denied exception
* /
var 路径= ProcessExtension.GetModuleFileNameEx(proc.Handle,proc.Id);
plt.ProcessLocation = path; // proc.MainModule.FileName;
}
catch (例外){}
尝试
{
plt.ProcessFileDescription = proc.MainModule.FileVersionInfo.FileDescription;
}
catch (例外){}
findProcessById.Add(pid,plt);
OnProcessChanged(plt,ProcessStatus.Added);
}
我在后台工作者中使用此代码。它监视进程。
我还使用了WMI Win32_Processes,我无法从中获得任何结果。
我的系统平台是64位和我的进程可能是32位或64位。
我怎么能这样做,独立于正在运行的程序平台(32b或64b)?
谢谢高级。
+ proc.StartTime.Ticks;
findCurrentProcessById。添加(pid,proc);
}
catch (例外){继续 ;}
if (findProcessById.ContainsKey(pid))
continue ;
// 流程已开始!
var plt = new ProcessLifeTime()
{
PID = proc.Id,
Process = proc,
ProcessName = proc.ProcessName,
StartTime = proc.StartTime,
};
尝试
{
/ * 在这一行中有时会抛出异常。或者当我使用WMI时路径为空或有时抛出Access is Denied exception
* /
var 路径= ProcessExtension.GetModuleFileNameEx(proc.Handle,proc.Id);
plt.ProcessLocation = path; // proc.MainModule.FileName;
}
catch (例外){}
尝试
{
plt.ProcessFileDescription = proc.MainModule.FileVersionInfo.FileDescription;
}
catch (例外){}
findProcessById.Add(pid,plt);
OnProcessChanged(plt,ProcessStatus.Added);
}
我在后台工作者中使用此代码。它监视进程。
我还使用了WMI Win32_Processes,我无法从中获得任何结果。
我的系统平台是64位和我的进程可能是32位或64位。
我怎么能这样做,独立于正在运行的程序平台(32b或64b)?
感谢高级。
从您的代码中,我从未见过使用System.Diagnostics.MainModule这样的基本内容
和模块属性System.Diagnostics.ProcessModule.FileName
。这正是您正在寻找的,这就是您应该开始的:
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.mainmodule%28v=vs.110%29.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.diagnostics.processmodule%28v=vs.110%29.aspx [ ^ ]。
对于32位或64位进程,它似乎完全无关。
-SA
Hi dears
I developed a process manager liked windows TaskManager. and now I want to get the process execution path.
I used the Process.MainMadule.FileName but the program throw an exception.
"Only part of a ReadProcessMemory or WriteProcessMemory request was completed"
My program platform target is AnyCPU and I used the .Net 2.0. Also I used the GetModuleFileNameEx API to get the process location, but some times it's returned null or Access is Denied Exception (Noted that my program run as admin).
My code is here:
var findCurrentProcessById = new Hashtable();
foreach (Process proc in Process.GetProcesses())
{
if (proc.Id == 0 || proc.Id == 4 || proc.ProcessName.ToLower() == "audiodg".ToLower())
continue;
string pid;
try
{
pid = proc.Id + "$" + proc.StartTime.Ticks;
findCurrentProcessById.Add(pid, proc);
}
catch (Exception) { continue; }
if (findProcessById.ContainsKey(pid))
continue;
// Process Started!
var plt = new ProcessLifeTime()
{
PID = proc.Id,
Process = proc,
ProcessName = proc.ProcessName,
StartTime = proc.StartTime,
};
try
{
/* In this line some times throw an exception. or when I use the WMI some times the path is empty or some times throw Access is Denied exception
*/
var path = ProcessExtension.GetModuleFileNameEx(proc.Handle, proc.Id);
plt.ProcessLocation = path;// proc.MainModule.FileName;
}
catch (Exception) { }
try
{
plt.ProcessFileDescription = proc.MainModule.FileVersionInfo.FileDescription;
}
catch (Exception) { }
findProcessById.Add(pid, plt);
OnProcessChanged(plt, ProcessStatus.Added);
}
I use this code in a background worker. Its monitor the processes.
Also I used the WMI Win32_Processes and I can't get any results from it.
My system platform is 64bit and my processes maybe are 32bit or 64bit.
How can I do this, independent of programs platform(32b or 64b) that are running?
Thanks in advanced.
" + proc.StartTime.Ticks; findCurrentProcessById.Add(pid, proc); } catch (Exception) { continue; } if (findProcessById.ContainsKey(pid)) continue; // Process Started! var plt = new ProcessLifeTime() { PID = proc.Id, Process = proc, ProcessName = proc.ProcessName, StartTime = proc.StartTime, }; try { /* In this line some times throw an exception. or when I use the WMI some times the path is empty or some times throw Access is Denied exception */ var path = ProcessExtension.GetModuleFileNameEx(proc.Handle, proc.Id); plt.ProcessLocation = path;// proc.MainModule.FileName; } catch (Exception) { } try { plt.ProcessFileDescription = proc.MainModule.FileVersionInfo.FileDescription; } catch (Exception) { } findProcessById.Add(pid, plt); OnProcessChanged(plt, ProcessStatus.Added); }
I use this code in a background worker. Its monitor the processes.
Also I used the WMI Win32_Processes and I can't get any results from it.
My system platform is 64bit and my processes maybe are 32bit or 64bit.
How can I do this, independent of programs platform(32b or 64b) that are running?
Thanks in advanced.
From your code, I never saw such a basic thing as usingSystem.Diagnostics.MainModule
and the module propertySystem.Diagnostics.ProcessModule.FileName
. This is exactly what you are looking for and this is what you should have started with:
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.mainmodule%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.diagnostics.processmodule%28v=vs.110%29.aspx[^].
As to 32-bit or 64-bit processes, it seems to be totally irrelevant.
—SA
这篇关于如何使用C#获取进程执行路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!