Exception When Accessing Process.StartTime, Exc:"Access is Denied"
-
Hi there, I have to get processes info for a system but I m having an Exception "Access is denied" in this code,
try { processes = Process.GetProcesses(mcName); int nThreadsCount = 0; foreach (Process p in processes) { string[] prcDetails = new string[]{p.ProcessName, p.Id.ToString(), // following statement causing exception p.StartTime.ToShortTimeString(), p.TotalProcessorTime.Duration().Hours.ToString()+":"+p.TotalProcessorTime.Duration().Minutes.ToString()+":"+p.TotalProcessorTime.Duration().Seconds.ToString(), p.Threads.Count.ToString(), p.HandleCount.ToString()}; nThreadsCount += p.Threads.Count; } } catch (Exception ex) { MessageBox.Show(ex.Message); }
When I commented the statementp.StartTime.ToShortTimeString(),
then next statement which is also getting-time-related propertyp.TotalProcessorTime.Duration().Hours.ToString()
is also causing same exception. Here is the Sysem Trace: " at System.Diagnostics.ProcessManager.OpenProcess(Int32 processId, Int32 access, Boolean throwIfExited)\r\n at System.Diagnostics.Process.GetProcessHandle(Int32 access, Boolean throwIfExited)\r\n at System.Diagnostics.Process.GetProcessTimes()\r\n at System.Diagnostics.Process.get_StartTime()\r\n at CustomizedTaskManager.frmmain.LoadAllProcessesOnStartup() in E:\\MyProjects\\Programing\\C#\\Task Manager\\CustomizedTaskManager\\CustomizedTaskManager\\Form1.cs:line 100" best regards, -
Hi there, I have to get processes info for a system but I m having an Exception "Access is denied" in this code,
try { processes = Process.GetProcesses(mcName); int nThreadsCount = 0; foreach (Process p in processes) { string[] prcDetails = new string[]{p.ProcessName, p.Id.ToString(), // following statement causing exception p.StartTime.ToShortTimeString(), p.TotalProcessorTime.Duration().Hours.ToString()+":"+p.TotalProcessorTime.Duration().Minutes.ToString()+":"+p.TotalProcessorTime.Duration().Seconds.ToString(), p.Threads.Count.ToString(), p.HandleCount.ToString()}; nThreadsCount += p.Threads.Count; } } catch (Exception ex) { MessageBox.Show(ex.Message); }
When I commented the statementp.StartTime.ToShortTimeString(),
then next statement which is also getting-time-related propertyp.TotalProcessorTime.Duration().Hours.ToString()
is also causing same exception. Here is the Sysem Trace: " at System.Diagnostics.ProcessManager.OpenProcess(Int32 processId, Int32 access, Boolean throwIfExited)\r\n at System.Diagnostics.Process.GetProcessHandle(Int32 access, Boolean throwIfExited)\r\n at System.Diagnostics.Process.GetProcessTimes()\r\n at System.Diagnostics.Process.get_StartTime()\r\n at CustomizedTaskManager.frmmain.LoadAllProcessesOnStartup() in E:\\MyProjects\\Programing\\C#\\Task Manager\\CustomizedTaskManager\\CustomizedTaskManager\\Form1.cs:line 100" best regards,What process are you trying to get to ? I guess access is denied.
Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
-
What process are you trying to get to ? I guess access is denied.
Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
Hi there, Thanx for reply Graus. This process is causing the exception. Name: msdtc MS DTCconsole program ImagePath: C:\WINDOWS\system32\msdtc.exe I m running this code in Vs 2005, While same code is running succesfully in VS 2003. I can't understand this weird behaviour. Here is complete code of my method, u can test it on VS03.
public static void ListAllProcesses() { Process[] processes = null; string mcName = "."; try { processes = Process.GetProcesses(mcName); foreach (Process p in processes) { Console.Write("\nProcessName: " + p.ProcessName); Console.Write("\tID: " + p.Id.ToString()); Console.Write("\tStartTime: " + p.StartTime.ToShortTimeString()); Console.Write("\tCPUTime: " + p.TotalProcessorTime.Duration().Hours.ToString()+":"+p.TotalProcessorTime.Duration().Minutes.ToString()+":"+p.TotalProcessorTime.Duration().Seconds.ToString()); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
best regards, Cyber Friend -
Hi there, I have to get processes info for a system but I m having an Exception "Access is denied" in this code,
try { processes = Process.GetProcesses(mcName); int nThreadsCount = 0; foreach (Process p in processes) { string[] prcDetails = new string[]{p.ProcessName, p.Id.ToString(), // following statement causing exception p.StartTime.ToShortTimeString(), p.TotalProcessorTime.Duration().Hours.ToString()+":"+p.TotalProcessorTime.Duration().Minutes.ToString()+":"+p.TotalProcessorTime.Duration().Seconds.ToString(), p.Threads.Count.ToString(), p.HandleCount.ToString()}; nThreadsCount += p.Threads.Count; } } catch (Exception ex) { MessageBox.Show(ex.Message); }
When I commented the statementp.StartTime.ToShortTimeString(),
then next statement which is also getting-time-related propertyp.TotalProcessorTime.Duration().Hours.ToString()
is also causing same exception. Here is the Sysem Trace: " at System.Diagnostics.ProcessManager.OpenProcess(Int32 processId, Int32 access, Boolean throwIfExited)\r\n at System.Diagnostics.Process.GetProcessHandle(Int32 access, Boolean throwIfExited)\r\n at System.Diagnostics.Process.GetProcessTimes()\r\n at System.Diagnostics.Process.get_StartTime()\r\n at CustomizedTaskManager.frmmain.LoadAllProcessesOnStartup() in E:\\MyProjects\\Programing\\C#\\Task Manager\\CustomizedTaskManager\\CustomizedTaskManager\\Form1.cs:line 100" best regards,If you are getting the error on Vista or Windows 7 it can be caused by UAC. See this blog post for a workaround: Bugs in System.Diagnostics.Process Class
Giorgi Dalakishvili #region signature My Articles Browsing xkcd in a windows 7 way[^] #endregion