【问题标题】:Opening Folders打开文件夹
【发布时间】:2015-07-15 11:40:26
【问题描述】:

我正在使用此代码打开一个文件夹:

If File.Exists(fileNameAndPath) Then
  Process.Start("explorer.exe", Path.GetDirectoryName(fileNameAndPath))
End If

但是这段代码不断打开同一文件夹的新窗口。 如何检查文件夹是否先打开然后打开。如果文件夹已经打开,那么只需将其放在前面? 是否可以突出显示我存在的文件? 我正在使用 .NET 2 谢谢

【问题讨论】:

    标签: .net vb.net file directory


    【解决方案1】:

    可以迭代打开的资源管理器窗口,您必须使用ShellWindows object。开始使用项目 > 添加参考 > 浏览 > 选择 c:\windows\system32\shdocvw.dll

    像这样使用它:

    Public Shared Sub OpenExplorerWindow(ByVal fileNameAndPath As String)
        Dim folder = System.IO.Path.GetDirectoryName(fileNameAndPath)
        Dim target As New Uri("file://" + folder)
        Dim windows As New SHDocVw.ShellWindows
        For Each win As SHDocVw.InternetExplorer In windows
            Dim loc As New Uri(win.LocationURL)
            If loc.Equals(target) Then
                SetForegroundWindow(New IntPtr(win.HWND))
                Exit Sub
            End If
        Next
        Process.Start("explorer.exe", folder)
    End Sub
    
    Private Declare Function SetForegroundWindow Lib "user32.dll" (hWnd As IntPtr) As Boolean
    

    【讨论】:

      猜你喜欢
      • 2012-09-21
      • 2013-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-21
      • 1970-01-01
      • 2016-11-17
      • 2015-07-07
      相关资源
      最近更新 更多