-1

I got this error:

(I can give more info from inspector)

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

There is the code:

(I was running this program as administrator since the beginning)

The correct answer is how to ignore exceptions, because the error occurs because certain processes cannot be read even if you have administrator privileges

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();
            }
        }
    }
}
Community
  • 1
  • 1
Cicker
  • 19
  • 11

2 Answers2

1

You don't need to declare a class inheriting System.Runtime.InteropServices.ExternalException. You just need to use try...catch to catch the exception System.ComponentModel.Win32Exception. Just modify the code like this.

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();
    }
}
大陸北方網友
  • 3,696
  • 3
  • 12
  • 37
0

Colud you try replace the catch exception as Exception?

catch (Exception)
   {
       continue;
   }

Another way if you want to mantain the Win32Exception is to change the inheritance of your custom exception to

public class Win32Exception : System.ComponentModel.Win32Exception
svladimirrc
  • 216
  • 1
  • 6