如何检索从远程PC使用VB.NET打开EXCEL文件名

您好目前我有以下代码。 它从进程中检索打开的excel文件的文件名并显示它。

现在它可以从我自己的电脑检索到,但是当我想从其他电脑远程检索它,它不起作用。

我已经授权访问,我实际上只能获得远程PC的进程ID和名称。

Dim w As Object Dim processQ As String Dim processes As Object Dim processA As Object Dim pname As String w = GetObject("winmgmts:{impersonationLevel=impersonate}\\" & pc & "\root\cimv2") processQ = "SELECT * FROM win32_process WHERE name = 'EXCEL.EXE'" processes = w.execquery(processQ) For Each processA In processes activeprocess = Process.GetProcessById(processA.processid) MsgBox(processA.processid & processA.name) Dim windows As IDictionary(Of IntPtr, String) = GetOpenWindowsFromPID(processA.processid) MsgBox(windows.Count()) For Each kvp As KeyValuePair(Of IntPtr, String) In windows Dim value As String = kvp.Value.ToString If InStr(value, "Excel") = False Then MsgBox(value) End If Next Next 

谁能告诉我该怎么办?这有什么问题?

 <DllImport("USER32.DLL")> Private Shared Function GetShellWindow() As IntPtr End Function <DllImport("USER32.DLL")> Private Shared Function GetWindowText(ByVal hwnd As IntPtr, ByVal lpString As StringBuilder, ByVal nMaxCount As Integer) As Integer End Function <DllImport("USER32.DLL")> Private Shared Function GetWindowTextLength(ByVal hwnd As IntPtr) As Integer End Function <DllImport("USER32.DLL", SetLastError:=True)> Private Shared Function GetWindowThreadProcessId(ByVal hwnd As IntPtr, <Out()> ByRef lpdwProcessId As UInt32) As UInt32 End Function <DllImport("USER32.DLL")> Private Shared Function IsWindowVisible(ByVal hwnd As IntPtr) As Boolean End Function Private Delegate Function EnumWindowsProc(ByVal hwnd As IntPtr, ByVal lParam As Integer) As Boolean <DllImport("USER32.DLL")> Private Shared Function EnumWindows(ByVal enumFunc As EnumWindowsProc, ByVal lParam As Integer) As Boolean End Function Private hShellWindow As IntPtr = GetShellWindow() Private dictWindows As New Dictionary(Of IntPtr, String) Private currentProcessID As Integer Public Function GetOpenWindowsFromPID(ByVal processID As Integer) As IDictionary(Of IntPtr, String) dictWindows.Clear() currentProcessID = processID EnumWindows(AddressOf enumWindowsInternal, 0) Return dictWindows End Function Public Function enumWindowsInternal(ByVal hwnd As IntPtr, ByVal lParam As Integer) As Boolean If (hwnd <> hShellWindow) Then Dim windowPid As UInt32 If Not IsWindowVisible(hwnd) Then Return True End If Dim length As Integer = GetWindowTextLength(hwnd) If (length = 0) Then Return True End If GetWindowThreadProcessId(hwnd, windowPid) If (windowPid <> currentProcessID) Then Return True End If Dim stringBuilder As New StringBuilder(length) GetWindowText(hwnd, stringBuilder, (length + 1)) dictWindows.Add(hwnd, stringBuilder.ToString) End If Return True End Function 

对不起,我很新vb.net ..但我在学习谢谢!