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;
var plt = new ProcessLifeTime()
{
PID = proc.Id,
Process = proc,
ProcessName = proc.ProcessName,
StartTime = proc.StartTime,
};
try
{
var path = ProcessExtension.GetModuleFileNameEx(proc.Handle, proc.Id);
plt.ProcessLocation = path;
}
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.