简体   繁体   English

如何判断文件夹是否在资源管理器中打开

[英]How to tell if Folder is opened in explorer

I'm writing a script in Siemens NX using vb.net, which helps export drawing files.我正在使用 vb.net 在 Siemens NX 中编写脚本,这有助于导出图形文件。 The file will be exported into a folder, that is kind of bothersome to manually navigate to, so I want to open said folder up to the user after the export.该文件将被导出到一个文件夹中,手动导航有点麻烦,所以我想在导出后向用户打开该文件夹。

The target folder is in a plm safe, so the file needs to be checked in in order to be used by other people.目标文件夹位于 plm 保险箱中,因此需要签入该文件才能供其他人使用。

Process.Start(targetfolder) doesn't work, because the folder gets opened "dumb" without any of the plm safe functionality. Process.Start(targetfolder)不起作用,因为该文件夹在没有任何 plm 安全功能的情况下被“哑”打开。

Process.Start("explorer.exe", targetfolder) opens the folder using the proper safe environment, however it will open a second folder when I run it again. Process.Start("explorer.exe", targetfolder)使用适当的安全环境打开文件夹,但是当我再次运行它时它会打开第二个文件夹。 Ideally I would like the folder to only become active if it's already opened.理想情况下,我希望该文件夹仅在已打开时才处于活动状态。

I found that I could use我发现我可以使用

<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
    Private Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    End Function

... ...

If FindWindow(vbNullString, IO.Path.GetFileName(targetfolder)) = 0 Then
            Process.Start("explorer.exe", targetfolder)
        Else
            AppActivate(IO.Path.GetFileName(targetfolder))
        End If

In order to only open a new window if there isn't one already.为了只在没有新窗口的情况下打开一个新窗口。 However, this also doesn't work reliably, because IO.Path.GetFileName(targetfolder) returns only the folder's name without the entire path.但是,这也不能可靠地工作,因为IO.Path.GetFileName(targetfolder)仅返回文件夹的名称,而不返回整个路径。 This means that if the user has a folder opened, that has the same name as the target folder but sits in a different spot FindWindow(vbNullString, IO.Path.GetFileName(targetfolder)) will find the other folder to be opened and won't open the target folder.这意味着如果用户打开了一个与目标文件夹同名但位于不同位置的文件夹FindWindow(vbNullString, IO.Path.GetFileName(targetfolder))将找到要打开的另一个文件夹并获胜t 打开目标文件夹。 It essentially can't differentiate the two.它基本上无法区分两者。 Additionally, AppActivate(IO.Path.GetFileName(targetfolder)) doesn't work if there are two folders of the same name opened.此外,如果打开了两个同名文件夹, AppActivate(IO.Path.GetFileName(targetfolder))不起作用。

Another thing I found was that you cannot rename a directory using FileSystem.Rename() while a child folder of the target folder is opened.我发现的另一件事是,在打开目标文件夹的子文件夹时,您无法使用FileSystem.Rename()重命名目录。 I could use this to check if the target folder is opened, by trying to rename the parent folder, however this will create many many false positives.我可以通过尝试重命名父文件夹来使用它来检查目标文件夹是否已打开,但这会产生许多误报。

I am now officially out of ideas.我现在正式没有想法了。 I also cannot use the plm safe's API, because the NX-script doesn't allow the use of "foreign" dll files, such as the plm library.我也不能使用 plm safe 的 API,因为 NX 脚本不允许使用“外部”dll 文件,例如 plm 库。

How do I check if a specific path is already opened in an explorer window?如何检查特定路径是否已在资源管理器窗口中打开?

Taken from here , you could do something along those lines:这里开始,您可以按照以下方式做一些事情:

Private Function isPathOpenedInExplorer(myPath As String) As Boolean
    Dim shellWindows As SHDocVw.ShellWindows = New SHDocVw.ShellWindows()
    Dim filename As String

    For Each ie As SHDocVw.InternetExplorer In shellWindows
        filename = Path.GetFileNameWithoutExtension(ie.FullName).ToLower()
        If filename.Equals("explorer") Then
            If myPath = ie.LocationURL.Remove(0, 8).Replace("/", "\").Replace("%20", " ") Then
                Return True ' Found
            End If
        End If
    Next

    Return False 'Not Found
End Function

don't forget to Imports System.IO and Add reference to 'SHDocVw'不要忘记Imports System.IO添加对“SHDocVw”的引用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何判断解决方案的开放位置? - How can you tell where your solution is opened from? 如何从打开EXE的文件夹中打开文件。 VB - How to open a file from folder where EXE was opened. VB 如何打开 windows 资源管理器 window 并选择特定文件夹 - How do I open a windows explorer window with a specific folder selected 如何隐藏在解决方案资源管理器中显示的bin和obj文件夹 - How to hide bin and obj folder from being displayed in solution explorer 如何判断一个文件夹中的文件是否与另一个文件夹中的文件相同但已被重命名 - How to tell if file in one folder is same as file on another folder but has been renamed VB 2010我如何使用返回随机子文件夹并在Windows资源管理器中打开该文件夹 - VB 2010 How can i use to return a random sub folder and open that folder in windows explorer 如何获取从Visual Basic 2013中的OpenFileDialog打开的文件的文件夹路径 - How to get folder path of file opened from OpenFileDialog in Visual Basic 2013 VB.NET程序在Internet Explorer打开时被锁定 - VB.NET Program Locks Up with Internet Explorer Opened 如何在用户窗体中显示单个Windows资源管理器文件夹(无法导航离开)? - How to display a single Windows Explorer folder (that cannot be navigated away from) within a userform? 如何更改我使用 VB 创建的文件夹的文件资源管理器图标 - How do I change the File Explorer icon for a folder I'm creating with VB
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM