我想做的是使用一种方法获取每个进程的处理时间
//Current Method
public string GetRunningTime(Process p){
string returnString = "00:00:00:00.00";//DD:HH;MM;SS.ff
try{
returnString = DateTime.Now.Subtract(p.StartTime).ToString(@"dd\.hh\:mm\:ss\.ff");//Time now - StartTime. ToString With format
}catch(Exception){}//Catchs System.ComponentModel.Win32Exception: 'Access is denied'
return returnString;//returns the string.
}
而且 try-catch 是我在不崩溃的情况下进行数学运算的唯一方法。我想知道是否有办法知道程序是否有权查看 StartTime。所以它会知道不要做数学。
//Example Method
public string GetRunningTime(Process p){
string returnString = "00:00:00:00.00";
if(p.HasAcessToStartTime){//Trying to immitate
returnString = DateTime.Now.Subtract(p.StartTime).ToString(@"dd\.hh\:mm\:ss\.ff");
}
return returnString;
}
最佳答案
根据官方文档,StartTime
仅通过异常向您显示是否有问题。
NotSupportedException
You are attempting to access the StartTime property for a process that is running on a remote computer. This property is available only for processes that are running on the local computer.
InvalidOperationException
The process has exited.
OR
The process has not been started.
Win32Exception
An error occurred in the call to the Windows function.
有几个选项可以解释这个问题:
- 请确保您的可执行文件以管理员身份运行,否则您的程序可能会被限制访问某些进程。
- 正如@Çöđěxěŕ 正确提到的,检查您是否正在尝试从
32 位
调用64 位
进程。 - this 中提到的一些进程(例如
svchost
系统进程)答案可能是那样的。
关于c# - 在C#中获取进程信息的权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63727141/