获取进程信息时出现System.ArgumentException和System.ComponentModel.Win32Exception

时间:2011-12-03 18:14:04

标签: c# argumentexception win32exception

当我尝试编写流程时#39;控制台的信息我得到System.ArgumentException和System.ComponentModel.Win32Exception。是什么导致这个?我怎么能停止那些?

        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) { }


            }

错误发生在&#34;字符串companyName = processListe [i] .MainModule.FileVersionInfo.CompanyName;&#34;线。

错误讯息:

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

最后,我提供了这些过程的信息输出,这让我得到了错误:

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

3 个答案:

答案 0 :(得分:7)

简短的回答是你无法摆脱异常。我在运行此代码时看到了一些例外情况,我没有在文档中明确指出:

  1. Win32Exception - 拒绝访问:进程以用户身份运行,您当前的用户无权访问该进程。请注意,即使以管理员身份运行,您也无法访问所有进程(例如,由于DRM限制而导致audiodg.exe)
  2. Win32Exception - 32位进程无法访问64位进程的模块
  3. Win32Exception - 无法枚举进程模块 - 我看到这发生在伪进程System和Idle上 - 它们不是真正的进程(它们是内核服务的占位符)并且没有任何要列出的模块。 / LI>

答案 1 :(得分:4)

According to Microsoft,如果流程在您致电ArgumentException的时间与您访问Process.GetProcesses()

之间退出,则会获得processLite[i].MainModule

检查processLite[i].HasExited可能会有所帮助,但无法保证,因为在您拨打下一个电话之前,仍有足够的时间让该进程退出。

答案 2 :(得分:2)

只是一个想法,但是当你输出信息时,你是否应该确保进程仍在运行?我认为列表可能只是对进程的引用,当您尝试访问属性时,它会尝试重新调用现在不存在的进程。