如何读取所有当前进程并在控制台中打印它们? / C# 控制台应用程序 System.Diagnostics
How can I read all current processes and print them in console? / C# ConsoleApp System.Diagnostics
我收到这个错误:
(我可以向检查员提供更多信息)
System.ComponentModel.Win32Exception: 'access denied'
The exception was originally generated in this call stack:
System.Diagnostics.ProcessManager.OpenProcess(int, int, bool)
System.Diagnostics.Process.GetProcessHandle(int, bool)
System.Diagnostics.Process.GetProcessTimes()
System.Diagnostics.Process.StartTime.get()
ConsoleApp1.Program.Main(string[]) on Program.cs
有代码:
(我从一开始就是 运行 这个程序的管理员)
正确答案是如何忽略异常,因为即使有管理员权限也无法读取某些进程而出现错误
using System.Diagnostics;
namespace ConsoleApp1
{
class Program
{
public class Win32Exception : System.Runtime.InteropServices.ExternalException
{
static void Main(string[] args)
{
var moment = DateTime.Now;
String rh = moment.Hour.ToString();
String rm = moment.Minute.ToString();
String rs = moment.Second.ToString();
Console.Title = moment.ToString("HH:mm:ss");
Process[] localAll = Process.GetProcesses();
/// Process[] procesos;
///procesos = Process.GetProcesses();
///
foreach (Process p in localAll)
{
/// Console.WriteLine(p.ProcessName);
try
{
var tmp = p.StartTime;
String h = p.StartTime.ToString("HH");
String m = p.StartTime.ToString("mm");
String s = p.StartTime.ToString("ss");
int x = Int32.Parse(rh);
int y = Int32.Parse(h);
if (x <= y)
{
Console.WriteLine($"{p.ProcessName} TIME= {p.StartTime.ToString("HH:mm:ss")}");
}
}
catch (Win32Exception)
{
continue;
}
}
Console.ReadKey();
}
}
}
}
你会尝试将 catch 异常替换为 Exception
吗?
catch (Exception)
{
continue;
}
如果你想保持 Win32Exception
的另一种方法是将自定义异常的继承更改为
public class Win32Exception : System.ComponentModel.Win32Exception
您无需声明 class 继承 System.Runtime.InteropServices.ExternalException
。您只需要使用 try...catch
来捕获异常 System.ComponentModel.Win32Exception
。像这样修改代码即可。
class Program
{
static void Main(string[] args)
{
// Code omitted
// ...
// Code omitted
foreach (Process p in localAll)
{
try
{
// Code omitted
// ...
// Code omitted
}
catch (System.ComponentModel.Win32Exception)
{
continue;
}
}
Console.ReadKey();
}
}
我收到这个错误:
(我可以向检查员提供更多信息)
System.ComponentModel.Win32Exception: 'access denied'
The exception was originally generated in this call stack:
System.Diagnostics.ProcessManager.OpenProcess(int, int, bool)
System.Diagnostics.Process.GetProcessHandle(int, bool)
System.Diagnostics.Process.GetProcessTimes()
System.Diagnostics.Process.StartTime.get()
ConsoleApp1.Program.Main(string[]) on Program.cs
有代码:
(我从一开始就是 运行 这个程序的管理员)
正确答案是如何忽略异常,因为即使有管理员权限也无法读取某些进程而出现错误
using System.Diagnostics;
namespace ConsoleApp1
{
class Program
{
public class Win32Exception : System.Runtime.InteropServices.ExternalException
{
static void Main(string[] args)
{
var moment = DateTime.Now;
String rh = moment.Hour.ToString();
String rm = moment.Minute.ToString();
String rs = moment.Second.ToString();
Console.Title = moment.ToString("HH:mm:ss");
Process[] localAll = Process.GetProcesses();
/// Process[] procesos;
///procesos = Process.GetProcesses();
///
foreach (Process p in localAll)
{
/// Console.WriteLine(p.ProcessName);
try
{
var tmp = p.StartTime;
String h = p.StartTime.ToString("HH");
String m = p.StartTime.ToString("mm");
String s = p.StartTime.ToString("ss");
int x = Int32.Parse(rh);
int y = Int32.Parse(h);
if (x <= y)
{
Console.WriteLine($"{p.ProcessName} TIME= {p.StartTime.ToString("HH:mm:ss")}");
}
}
catch (Win32Exception)
{
continue;
}
}
Console.ReadKey();
}
}
}
}
你会尝试将 catch 异常替换为 Exception
吗?
catch (Exception)
{
continue;
}
如果你想保持 Win32Exception
的另一种方法是将自定义异常的继承更改为
public class Win32Exception : System.ComponentModel.Win32Exception
您无需声明 class 继承 System.Runtime.InteropServices.ExternalException
。您只需要使用 try...catch
来捕获异常 System.ComponentModel.Win32Exception
。像这样修改代码即可。
class Program
{
static void Main(string[] args)
{
// Code omitted
// ...
// Code omitted
foreach (Process p in localAll)
{
try
{
// Code omitted
// ...
// Code omitted
}
catch (System.ComponentModel.Win32Exception)
{
continue;
}
}
Console.ReadKey();
}
}