实现"打开包含文件夹"并突出显示文件

dev*_*os1 21 .net c# file windows-explorer

这可以是在与文件/文件夹一起使用的程序中具有的便利功能.使用以下方法实际打开包含文件夹很容易:

System.Diagnostics.Process.Start( *path to folder* );
Run Code Online (Sandbox Code Playgroud)

...但是如何在该父文件夹中实际选择目标文件?如果我使用Process.Start方法,它实际上会尝试打开该文件.

Reg*_*ent 50

根据Windows资源管理器命令行选项,您只需要explorer使用/select参数启动进程.

例如,' explorer /select,c:\Windows'将打开一个包含c:\windows所选文件夹的窗口.

所以Process.Start("explorer.exe", "/select," + filename)应该足够了.


Chr*_*lor 5

使用/select, "filename"命令行参数执行Explorer.exe

System.Diagnostics.Process.Start(
    "explorer.exe", 
    string.Format("/select, \"{0}\"", filename));
Run Code Online (Sandbox Code Playgroud)

  • 只是为了一个注释,引用(`\"{0} \"`)是没有必要的,因为`explorer`会把`/ select,`之后的任何东西视为路径(忽略开始和结束的空格) - 所以'`/ select ,{0}`'就够了...... (3认同)