如何打开资源管理器与特定文件选择? [英] How to open Explorer with a specific file selected?

查看:235
本文介绍了如何打开资源管理器与特定文件选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想代码,你可以通过一个文件路径的功能,例如:

I would like to code a function to which you can pass a file path, for example:

C:\FOLDER\SUBFOLDER\FILE.TXT

和它会打开Windows资源管理器包含文件的文件夹然后选择文件夹内的文件。 (类似于许多程序中使用的显示文件夹的概念。)

and it would open Windows Explorer with the folder containing the file and then select this file inside the folder. (Similar to the "Show In Folder" concept used in many programs.)

我怎样才能做到这一点?

How can I do this?

推荐答案

不使用Win32外壳功能最简单的方法是简单地用 /选择参数启动Explorer.exe的。例如,启动过程

Easiest way without using Win32 shell functions is to simply launch explorer.exe with the /select parameter. For example, launching the process

的Explorer.exe /select,\"C:\Folder\subfolder\file.txt\"

将打开一个新的浏览器窗口到C:\Folder\subfolder与选定file.txt的

will open a new explorer window to C:\Folder\subfolder with file.txt selected.

建立了狄格兰的例子(检查省略错误):

Building off Tigran's example (error checking omitted):

public void OpenFolder(string filePath) {
    Process.Start("explorer.exe", string.Format("/select,\"{0}\"", filePath));
}

如果您希望以编程方式做到这一点,而无需启动一个新的进程,你会需要使用shell函数 SHOpenFolderAndSelectItems ,这是 /选择命令的explorer.exe将在内部使用的。请注意,这需要使用PIDL或者,并且可以是一个真正皮塔饼,如果你不熟悉的外壳API的工作。

If you wish to do it programmatically without launching a new process, you'll need to use the shell function SHOpenFolderAndSelectItems, which is what the /select command to explorer.exe will use internally. Note that this requires the use of PIDLs, and can be a real PITA if you are not familiar with how the shell APIs work.

这篇关于如何打开资源管理器与特定文件选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆