簡體   English   中英

如何讀取所有當前進程並在控制台中打印它們? / C# ConsoleApp System.Diagnostics

[英]How can I read all current processes and print them in console? / C# ConsoleApp System.Diagnostics

我收到此錯誤:

(我可以從檢查員那里提供更多信息)

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

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

您嘗試將 catch 異常替換為Exception嗎?

catch (Exception)
   {
       continue;
   }

如果您想保留Win32Exception另一種方法是將自定義異常的繼承更改為

public class Win32Exception : System.ComponentModel.Win32Exception

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM