Home > Back-end >  Check folder is open or not
Check folder is open or not

Time:12-28

how should i make this go open folder only if only folder it is not open? the folder should only open if it is not open. and put an if, and an 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

CodePudding user response:

If the goal is to not open multiple explorer instances for the same directory, then you can simply pass a new ProcessStartInfo object to the Process.Start(...) function. Assign the directory path to the ProcessStartInfo.FileName property and the "open" command to the ProcessStartInfo.Verb property. This way, an already open instance will be activated rather than opening a new one for the same dir.

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

On the other hand, if you still need to find out whether a directory is already open in the explorer, then you could pinvoke the FindWindowByCaption function which returns a handle to a window if any.

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

Of course, the target directory should exist in the first place. Just in case, see the DirectoryInfo.Exists property and the DirectoryInfo.Create method.

  • Related