System.ArgumentException und System.comPonentModel.win32Exception beim Erhalten von Prozessinformationen

StackOverflow https://stackoverflow.com/questions/8369811

Frage

Wenn ich versuche, Prozessinformationen in Konsole zu schreiben, erhalte ich System.ArgumentException und System.comPonentModel.win32Exception. Was verursacht das? Wie kann ich aufhören, diese zu haben?

        Process processListe = Process.GetProcesses();


            for (int i = 0; i < processListe.Count(); i++)
            {
                try
                {
                string companyName = processListe[i].MainModule.FileVersionInfo.CompanyName;
                string fileVersion = processListe[i].MainModule.FileVersionInfo.FileVersion;

                Console.WriteLine(companyName  + " " + fileVersion);


                }
                catch (Exception) { }


            }

Fehler erfolgen in "String companyName = processListe [i] .Mainmodule.FileversionInfo.comPanyname;" Linie.

Fehlermeldungen:

   System.ArgumentException: Illegal characters in path.
   at System.Security.Permissions.FileIOPermission.HasIllegalCharacters(String[] str)
   at System.Security.Permissions.FileIOPermission.AddPathList(FileIOPermissionAccess access, AccessControlActions control, String[] pathListOrig, Boolean checkForDuplicates, Boolean needFullPath, Boolean copyPathList)
   at System.Security.Permissions.FileIOPermission..ctor(FileIOPermissionAccess access, String[] pathList, Boolean checkForDuplicates, Boolean needFullPath)
   at System.IO.Path.GetFullPath(String path)
   at System.Diagnostics.FileVersionInfo.GetFullPathWithAssert(String fileName)
   at System.Diagnostics.FileVersionInfo.GetVersionInfo(String fileName)
   at System.Diagnostics.ProcessModule.get_FileVersionInfo()


   A first chance exception of type 'System.ComponentModel.Win32Exception' occurred in System.dll
   System.ComponentModel.Win32Exception (0x80004005): Unable to enumerate the process modules.
   at System.Diagnostics.NtProcessManager.GetModuleInfos(Int32 processId, Boolean firstModuleOnly)
   at System.Diagnostics.NtProcessManager.GetFirstModuleInfo(Int32 processId)
   at System.Diagnostics.Process.get_MainModule()

Zuletzt habe ich eine Informationsausgabe des Prozesses erstellt, wodurch ich Fehler bekomme:

    Exception: Illegal characters in path.
    Proess Name: winlogon Company Name: Aestan Software Version: 1.6.1.33
    Detail: System.ArgumentException: Illegal characters in path.
   at System.Security.Permissions.FileIOPermission.HasIllegalCharacters(String[] str)
   at System.Security.Permissions.FileIOPermission.AddPathList(FileIOPermissionAccess access, AccessControlActions control, String[] pathListOrig, Boolean checkForDuplicates, Boolean needFullPath, Boolean copyPathList)
   at System.Security.Permissions.FileIOPermission..ctor(FileIOPermissionAccess access, String[] pathList, Boolean checkForDuplicates, Boolean needFullPath)
   at System.IO.Path.GetFullPath(String path)
   at System.Diagnostics.FileVersionInfo.GetFullPathWithAssert(String fileName)
   at System.Diagnostics.FileVersionInfo.GetVersionInfo(String fileName)
   at System.Diagnostics.ProcessModule.get_FileVersionInfo()

    Exception: Illegal characters in path.
    Proess Name: csrss Company Name: Microsoft Corporation Version: 2009.0100.1600.01 ((KJ_RTM).100402-1540 )
    Detail: System.ArgumentException: Illegal characters in path.
at System.Security.Permissions.FileIOPermission.HasIllegalCharacters(String[] str)
   at System.Security.Permissions.FileIOPermission.AddPathList(FileIOPermissionAccess access, AccessControlActions control, String[] pathListOrig, Boolean checkForDuplicates, Boolean needFullPath, Boolean copyPathList)
   at System.Security.Permissions.FileIOPermission..ctor(FileIOPermissionAccess access, String[] pathList, Boolean checkForDuplicates, Boolean needFullPath)
   at System.IO.Path.GetFullPath(String path)
   at System.Diagnostics.FileVersionInfo.GetFullPathWithAssert(String fileName)
   at System.Diagnostics.FileVersionInfo.GetVersionInfo(String fileName)
   at System.Diagnostics.ProcessModule.get_FileVersionInfo()

    Exception: Unable to enumerate the process modules.
    Proess Name: System Company Name: BitTorrent, Inc. Version: 7.5.0.25682
    Detail: System.ComponentModel.Win32Exception (0x80004005): Unable to enumerate the process modules. 
at System.Diagnostics.NtProcessManager.GetModuleInfos(Int32 processId, Boolean firstModuleOnly)
   at System.Diagnostics.NtProcessManager.GetFirstModuleInfo(Int32 processId)
   at System.Diagnostics.Process.get_MainModule()

    Exception: Access is denied
    Proess Name: Cheat Engine Company Name:  Version: 5.6.1.10
    Detail:  System.ComponentModel.Win32Exception (0x80004005): Access is denied
   at System.Diagnostics.ProcessManager.OpenProcess(Int32 processId, Int32 access, Boolean throwIfExited)
   at System.Diagnostics.Process.GetProcessHandle(Int32 access, Boolean throwIfExited)
   at System.Diagnostics.Process.get_HasExited()
War es hilfreich?

Lösung

Die kurze Antwort ist, dass Sie die Ausnahmen nicht loswerden können. Es gibt einige Ausnahmen, die ich sehe, wenn ich diesen Code ausführe, den ich in der Dokumentation nicht explizit herausstellt:

  1. Win32Exception - Zugriff verweigert: Der Prozess wird als Benutzer ausgeführt und Ihr aktueller Benutzer hat kein Recht, auf den Prozess zuzugreifen. Beachten Sie auch, wenn Sie als Administrator ausgeführt werden, Sie haben keinen Zugriff auf alle Prozesse (z. B. audiodg.exe aufgrund von DRM -Beschränkungen)
  2. Win32Exception - 32 -Bit -Prozesse können nicht auf Module eines 64 -Bit -Prozesses zugreifen
  3. Win32Exception - Die Prozessmodule können nicht aufgezählt werden - Ich sehe dies auf dem Pseudo -Prozessystem und im Leerlauf - sie sind keine echten Prozesse (sie sind Platzinhaber für Kerneldienste) und haben keine Module zur Auflistung.

Andere Tipps

Laut Microsoft, du erhältst ArgumentException Wenn der Prozess zwischen der Zeit, die Sie angerufen haben Process.GetProcesses() und die Zeit, in der Sie zugreifen processLite[i].MainModule

Überprüfung processLite[i].HasExited kann helfen, aber es ist nicht garantiert, da es noch genügend Zeit hat, um den Prozess zu beenden, bevor Sie den nächsten Anruf tätigen.

Nur ein Gedanke, aber sollten Sie nicht sicherstellen, dass der Prozess noch ausgeführt wird, wenn Sie die Informationen ausgeben? Ich denke, dass die Liste möglicherweise nur Verweise auf den Prozess sein könnte, und wenn Sie versuchen, auf die Eigenschaften zuzugreifen, versucht sie, den jetzt nicht existierenden Prozess erneut zu berücksichtigen.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top