Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在选定特定文件的情况下打开资源管理器?_C#_Winapi_Vbscript - Fatal编程技术网

C# 如何在选定特定文件的情况下打开资源管理器?

C# 如何在选定特定文件的情况下打开资源管理器?,c#,winapi,vbscript,C#,Winapi,Vbscript,我想编写一个可以向其传递文件路径的函数,例如: C:\FOLDER\SUBFOLDER\FILE.TXT 它会打开包含该文件的文件夹的Windows资源管理器,然后在文件夹中选择该文件。(类似于许多程序中使用的“在文件夹中显示”概念。) 如何执行此操作?不使用Win32 shell函数的最简单方法是使用/select参数启动explorer.exe。例如,启动流程 explorer.exe/选择“C:\Folder\subfolder\file.txt” 将打开一个新的资源管理器窗口,打开C:

我想编写一个可以向其传递文件路径的函数,例如:

C:\FOLDER\SUBFOLDER\FILE.TXT
它会打开包含该文件的文件夹的Windows资源管理器,然后在文件夹中选择该文件。(类似于许多程序中使用的“在文件夹中显示”概念。)


如何执行此操作?

不使用Win32 shell函数的最简单方法是使用
/select
参数启动explorer.exe。例如,启动流程

explorer.exe/选择“C:\Folder\subfolder\file.txt”

将打开一个新的资源管理器窗口,打开C:\Folder\subfolder并选择file.txt

如果希望以编程方式执行此操作而不启动新进程,则需要使用shell函数,explorer.exe的
/select
命令将在内部使用此函数。请注意,这需要使用PIDL,如果您不熟悉shell API的工作方式,则可以使用真正的PITA

下面是
/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;
}

参考:

执行命令时,如果路径包含多个斜杠,则不会打开文件夹并正确选择文件 请确保您的文件路径如下所示

C:\a\b\x.txt

而不是

C:\\a\\b\\x.txt

自Windows XP以来(即Windows 2000或更早版本不支持)支持的方法是:


跟进@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 );

在选择之后需要一个逗号,如explorer.exe/select,“c:\folder\subfolder\file.txt启动新进程的缺点是什么?@Gusdor什么都没有。除非在文件夹视图选项中的单独进程中启用了启动文件夹窗口(默认情况下,它是禁用的)这实际上并不会启动新进程。它会自动运行,但会调用现有的explorer.exe实例来处理请求,而不是在单独的内存空间中启动新进程。如果您经常调用此代码,则在执行过程中使用
shopenfolder和selectItems
肯定会更高效、更少浪费不过,开头的顺序。@MahmoudAl Qudsi不是使用
Regex
来清理文件路径,而是可以使用
filePath=System.IO.path.GetFullPath(filePath);
。它将规范化目录分隔符,还将解析相对路径,例如,如果传入类似
@.\some\relative\file\path.txt“
@”C:\Some\relative\..\file\path.txt”
。您还可以更新示例以使用新的字符串插值:
Process.Start(“explorer.exe”,$”/select,\“{filePath}\”)如果文件名本身包含一个coma,这似乎不起作用。另外,是否可以选择多个文件?可能重复的可能重复的
// 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 );