僅當檔案夾未打開時,我應該如何使這個檔案夾打開?該檔案夾僅應在未打開時打開。并放置一個 if 和一個 else。
Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
End Function
Dim folderpath As String
Dim foldername As String
'Process.Start(System.Environment.CurrentDirectory)
folderpath = My.Application.Info.DirectoryPath ("\Check")
foldername = System.IO.Path.GetFileName(folderpath)
If FindWindow(vbNullString, foldername) = 0 Then
Process.Start("explorer.exe", folderpath)
End If
uj5u.com熱心網友回復:
Windows 內置了一個功能來執行此操作。https://docs.microsoft.com/en-us/windows/win32/shell/ishelldispatch-windows并查看windows屬性。
當 Internet Explorer 4 桌面更新發布時,本地檔案和 Internet 檔案之間沒有區別。因此,它列出了 Internet Explorer(但沒有其他瀏覽器)和 Windows Explorer 視窗。
該win中AllWindows實際上是一個Internet Explorer物件-見https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/platform-apis/aa752084(v=vs.85)
'ListOpenShellWindows.vb
Imports System.Runtime.InteropServices
Public Module MyApplication
Sub Main()
Dim ObjShell as Object
Dim AllWindows as Object
objShell = CreateObject("Shell.Application")
AllWindows = objShell.Windows
For Each win in AllWindows
Msgbox(win.LocationUrl & " - " & win.LocationName)
Next
End Sub
End Module
要編譯,請將兩個檔案復制到同一檔案夾中,然后雙擊批處理檔案。
REM ListOpenShellWindows.bat
REM This file compiles ListOpenShellWindows.vb to ListOpenShellWindows.exe using the system VB.NET compiler
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc.exe" /target:winexe /out:"%~dp0\ListOpenShellWindows.exe" "%~dp0\ListOpenShellWindows.vb"
pause
編輯
如果與Shell互動,最好使用Shell函式。你ObjShell.Open用來打開一個檔案夾。請參閱https://docs.microsoft.com/en-us/windows/win32/shell/ishelldispatch-open。
uj5u.com熱心網友回復:
如果目標不是為同一目錄打開多個資源管理器實體,那么您可以簡單地將新ProcessStartInfo物件傳遞給該Process.Start(...)函式。將目錄路徑分配給ProcessStartInfo.FileName屬性并將"open"命令分配給ProcessStartInfo.Verb屬性。這樣,一個已經打開的實體將被激活,而不是為同一個目錄打開一個新的實體。
' Some caller...
Dim dirInfo = New DirectoryInfo(Path.Combine(My.Application.Info.DirectoryPath, "Check"))
Dim psi As New ProcessStartInfo With {
.FileName = dirInfo.FullName,
.Verb = "open"
}
Process.Start(psi)
在另一方面,如果你仍然需要找出一個目錄是否已經在瀏覽器中打開,那么你可以的PInvoke在FindWindowByCaption回傳的句柄視窗,如果任何功能。
Dim dirInfo = New DirectoryInfo(Path.Combine(My.Application.Info.DirectoryPath, "Check"))
Dim p = FindWindowByCaption(IntPtr.Zero, dirInfo.Name)
If p = IntPtr.Zero Then
Process.Start(dirInfo.FullName)
Else
Console.WriteLine("Already Open!")
End If
<DllImport("user32.dll", EntryPoint:="FindWindow", SetLastError:=True, CharSet:=CharSet.Auto)>
Private Shared Function FindWindowByCaption(zero As IntPtr, lpWindowName As String) As IntPtr
End Function
當然,目標目錄首先應該存在。以防萬一,請參閱DirectoryInfo.Exists屬性和DirectoryInfo.Create方法。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/402854.html
標籤:
