C# 如何使用选定的特定文件打开资源管理器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13680415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to open Explorer with a specific file selected?
提问by Jester
I would like to code a function to which you can pass a file path, for example:
我想编写一个可以传递文件路径的函数,例如:
C:\FOLDER\SUBFOLDER\FILE.TXT
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.)
它将使用包含该文件的文件夹打开 Windows 资源管理器,然后在文件夹中选择该文件。(类似于许多程序中使用的“在文件夹中显示”概念。)
How can I do this?
我怎样才能做到这一点?
采纳答案by Mahmoud Al-Qudsi
Easiest way without using Win32 shell functions is to simply launch explorer.exe with the /selectparameter. For example, launching the process
不使用 Win32 shell 函数的最简单方法是简单地启动带有/select参数的explorer.exe 。例如,启动进程
explorer.exe /select,"C:\Folder\subfolder\file.txt"
explorer.exe /select,"C:\Folder\subfolder\file.txt"
will open a new explorer window to C:\Folder\subfolder with file.txt selected.
将打开一个新的资源管理器窗口到 C:\Folder\subfolder 并选择了 file.txt。
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 /selectcommand 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.
如果您希望在不启动新进程的情况下以编程方式执行此操作,则需要使用 shell 函数SHOpenFolderAndSelectItems,这是/selectexplorer.exe 将在内部使用的命令。请注意,这需要使用 PIDL,如果您不熟悉 shell API 的工作方式,则可以是一个真正的 PITA。
Here's a complete, programmatic implementation of the /selectapproach, with path cleanup thanks to suggestions from @Bhushan and @tehDorf:
这是该/select方法的一个完整的、程序化的实现,由于@Bhushan 和@tehDorf 的建议进行了路径清理:
public bool ExploreFile(string filePath) {
if (!System.IO.File.Exists(filePath)) {
return false;
}
//Clean up file path so it can be navigated OK
filePath = System.IO.Path.GetFullPath(filePath);
System.Diagnostics.Process.Start("explorer.exe", string.Format("/select,\"{0}\"", filePath));
return true;
}
Reference: Explorer.exe Command-line switches
回答by Bhushan Pawar
When executing the command if your path contains multiple slashes then it will not open the folder and select the file properly Please make sure that your file path should be like this
执行命令时,如果您的路径包含多个斜杠,则不会打开文件夹并正确选择文件请确保您的文件路径应该是这样的
C:\a\b\x.txt
C:\a\b\x.txt
instead of
代替
C:\\a\\b\\x.txt
C:\\a\\b\\x.txt
回答by Ian Boyd
The supportedmethod since Windows XP (i.e. not supported on Windows 2000 or earlier) is SHOpenFolderAndSelectItems:
在支持自Windows XP(即不支持在Windows 2000或更早)的方法是SHOpenFolderAndSelectItems:
void OpenFolderAndSelectItem(String filename)
{
// Parse the full filename into a pidl
PIDLIST_ABSOLUTE pidl;
SFGAO flags;
SHParseDisplayName(filename, null, out pidl, 0, out flags);
try
{
// Open Explorer and select the thing
SHOpenFolderAndSelectItems(pidl, 0, null, 0);
}
finally
{
// Use the task allocator to free to returned pidl
CoTaskMemFree(pidl);
}
}
回答by M Katz
To follow up on @Mahmoud Al-Qudsi's answer. when he says "launching the process", this is what worked for me:
跟进@Mahmoud Al-Qudsi 的回答。当他说“启动流程”时,这对我有用:
// assume variable "path" has the full path to the file, but possibly with / delimiters
for ( int i = 0 ; path[ i ] != 0 ; i++ )
{
if ( path[ i ] == '/' )
{
path[ i ] = '\';
}
}
std::string s = "explorer.exe /select,\"";
s += path;
s += "\"";
PROCESS_INFORMATION processInformation;
STARTUPINFOA startupInfo;
ZeroMemory( &startupInfo, sizeof(startupInfo) );
startupInfo.cb = sizeof( STARTUPINFOA );
ZeroMemory( &processInformation, sizeof( processInformation ) );
CreateProcessA( NULL, (LPSTR)s.c_str(), NULL, NULL, FALSE, 0, NULL, NULL, &startupInfo, &processInformation );

