【问题标题】:How can I read all current processes and print them in console? / C# ConsoleApp System.Diagnostics如何读取所有当前进程并在控制台中打印它们? / C# ConsoleApp System.Diagnostics
【发布时间】:2020-06-15 02:26:09
【问题描述】:

我收到了这个错误:

(我可以向检查员提供更多信息)

System.ComponentModel.Win32Exception: '访问被拒绝'

异常最初是在这个调用栈中产生的:

System.Diagnostics.ProcessManager.OpenProcess(int, int, bool)

System.Diagnostics.Process.GetProcessHandle(int, bool)

System.Diagnostics.Process.GetProcessTimes()

System.Diagnostics.Process.StartTime.get()

Program.cs 上的 ConsoleApp1.Program.Main(string[])

有代码:

(我从一开始就以管理员身份运行这个程序)

正确的答案是如何忽略异常,因为即使有管理员权限也无法读取某些进程而发生错误

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();
            }
        }
    }
}

【问题讨论】:

  • Win32Exception 没有按我的预期工作

标签: c# visual-studio process system.diagnostics


【解决方案1】:

您不需要声明继承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();
    }
}

【讨论】:

    【解决方案2】:

    您尝试将捕获异常替换为Exception

    catch (Exception)
       {
           continue;
       }
    

    如果您想维护Win32Exception,另一种方法是将自定义异常的继承更改为

    public class Win32Exception : System.ComponentModel.Win32Exception
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-28
      • 1970-01-01
      • 2019-10-07
      相关资源
      最近更新 更多