检查文件夹是否打开
Check folder is open or not
只有当文件夹没有打开时,我应该如何让这个文件夹打开?该文件夹只有在未打开时才应打开。并放置一个 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
如果目标是不为同一目录打开多个资源管理器实例,那么您只需将新的 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
函数 returns window 的句柄(如果有的话)。
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
property and the DirectoryInfo.Create
方法。
Windows 有一个内置函数可以做到这一点。 https://docs.microsoft.com/en-us/windows/win32/shell/ishelldispatch-windows 并查看 windows
属性.
当 Internet Explorer 4 桌面更新发布时,本地文件和 Internet 文件之间没有区别。因此它列出了 Internet Explorer(但没有其他浏览器)和 Windows Explorer windows.
AllWindows
中的 win
实际上是一个 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。
只有当文件夹没有打开时,我应该如何让这个文件夹打开?该文件夹只有在未打开时才应打开。并放置一个 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
如果目标是不为同一目录打开多个资源管理器实例,那么您只需将新的 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
函数 returns window 的句柄(如果有的话)。
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
property and the DirectoryInfo.Create
方法。
Windows 有一个内置函数可以做到这一点。 https://docs.microsoft.com/en-us/windows/win32/shell/ishelldispatch-windows 并查看 windows
属性.
当 Internet Explorer 4 桌面更新发布时,本地文件和 Internet 文件之间没有区别。因此它列出了 Internet Explorer(但没有其他浏览器)和 Windows Explorer windows.
AllWindows
中的 win
实际上是一个 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。