首页 文章

VB.NET System32 Path FileNotFoundException

提问于
浏览
0

我有以下代码:

Imports System.Management

    Module Module1
        Private MicrosoftProcs As New List(Of String)
        Private Sub FindMicrosoftProcs()
            Dim searcher As New ManagementObjectSearcher("SELECT * FROM Win32_Process")
            For Each p2 As ManagementObject In searcher.Get()
                If p2("Name") <> "System" Or p2("Name") <> "System Idle Process" Or p2("Name") <> Process.GetCurrentProcess.ProcessName & ".exe" Then
                    Dim x As String = p2("ExecutablePath")
                    If Not x Is Nothing Then
                        If x.Length > 2 Then
                            Dim fvi As System.Diagnostics.FileVersionInfo = System.Diagnostics.FileVersionInfo.GetVersionInfo(p2("ExecutablePath").ToString)
                            Dim sDescription As String = fvi.CompanyName & "/" & fvi.LegalCopyright & "/" & fvi.LegalTrademarks & "/" & fvi.ProductName & "/" & fvi.FileDescription & "/"
                            If sDescription.ToLower.Contains("microsoft") Then
                                MicrosoftProcs.Add(p2("ExecutablePath"))
                                Debug.WriteLine("Microsoft process : " & p2("ExecutablePath"))
                            End If
                        End If
                    End If
                End If
            Next
        End Sub
    End Module

我正在使用64位Windows,但代码是针对32位Windows编译的(兼容性) . 如果我运行为64位编译的代码,我没有代码问题,但如果我运行它编译为32位我得到FileNotFoundException:

A first chance exception of type 'System.IO.FileNotFoundException' occurred in System.dll
    System.IO.FileNotFoundException: C:\Windows\system32\csrss.exe
       at System.Diagnostics.FileVersionInfo.GetVersionInfo(String fileName)
       at WindowsApplication1.Form1.Button1_Click(Object sender, EventArgs e) in C:\Users\Maximus\Documents\Visual Studio 2010\Projects\rupe\rupe\Form1.vb:line 81

我不知道如何解决它 . 你能帮我吗 ?提前致谢 .

1 回答

  • 0

    我似乎无法找到一个确切的答案,但我很确定问题是32位进程没有权限访问64位进程的模块 . 这就是为什么你的64位应用程序没有问题,但32位应用程序不喜欢某些进程 . 我能找到的最好的资源是另一个SO问题,你可以在这里阅读:System.ArgumentException and System.ComponentModel.Win32Exception when getting process information .

    也就是说,当您运行32位应用程序时,似乎没有办法获取这些64位进程的文件信息 . 如果您绝对需要此信息,则别无选择,只能将应用程序构建为64位 . 如果您真的不需要这些信息,并希望继续使用32位应用程序中可访问的进程,您可以尝试以下方法:

    Private Sub FindMicrosoftProcs()
        Try
            Dim searcher As New ManagementObjectSearcher("SELECT * FROM Win32_Process")
    
            For Each p2 As ManagementObject In searcher.Get()
                If p2("Name") <> "System" Or p2("Name") <> "System Idle Process" Or p2("Name") <> Process.GetCurrentProcess.ProcessName & ".exe" Then
                    Dim x As String = p2("ExecutablePath")
                    If Not x Is Nothing Then
                        If x.Length > 2 Then
                            Dim fvi As System.Diagnostics.FileVersionInfo
    
                            Try
                                fvi = System.Diagnostics.FileVersionInfo.GetVersionInfo(p2("ExecutablePath").ToString)
                                Dim sDescription As String = fvi.CompanyName & "/" & fvi.LegalCopyright & "/" & fvi.LegalTrademarks & "/" & fvi.ProductName & "/" & fvi.FileDescription & "/"
                                If sDescription.ToLower.Contains("microsoft") Then
                                    MicrosoftProcs.Add(p2("ExecutablePath"))
                                    Debug.WriteLine("Microsoft process : " & p2("ExecutablePath"))
                                End If
                            Catch ex As Exception
                                nSkipped += 1
                            End Try
                        End If
                    End If
                End If
            Next
    
            MessageBox.Show("Found " & MicrosoftProcs.Count & " and skipped " & nSkipped & " files.")
        Catch ex As Exception
            MessageBox.Show("Error:  " & ex.Message)
        End Try
    End Sub
    

相关问题