使用Process.Start打开文件夹

Dan*_*iel 140 c# explorer

我看到了另一个话题,我有另一个问题.该过程正在启动(在任务管理器中看到),但文件夹未在我的屏幕上打开.怎么了?

System.Diagnostics.Process.Start("explorer.exe", @"c:\teste");
Run Code Online (Sandbox Code Playgroud)

Fre*_*örk 243

你确定文件夹" c:\teste"存在吗?如果没有,资源管理器将打开显示一些默认文件夹(在我的情况下为" C:\Users\[user name]\Documents").

更新

我尝试了以下变化:

// opens the folder in explorer
Process.Start(@"c:\temp");
// opens the folder in explorer
Process.Start("explorer.exe", @"c:\temp");
// throws exception
Process.Start(@"c:\does_not_exist");
// opens explorer, showing some other folder)
Process.Start("explorer.exe", @"c:\does_not_exist");
Run Code Online (Sandbox Code Playgroud)

如果这些都没有(除了抛出异常的那个)在你的计算机上运行,​​我认为问题不在于代码,而是在环境中.如果是这种情况,我会尝试以下一个(或两个):

  • 打开"运行"对话框,输入"explorer.exe"并按Enter键
  • 打开命令提示符,键入"explorer.exe"并按Enter键

  • 如果该资源管理器窗口已打开,则差异较小:“Process.Start(path)”激活窗口(可能仅在任务栏中闪烁,不会显示在前面);`explorer.exe`+参数总是在前面打开一个新窗口(但多次打开同一窗口)。所以两者都有注意事项。 (2认同)

Ore*_*ost 43

为了完整起见,如果你想要打开一个文件夹,请使用:

System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo() {
    FileName = "C:\\teste\\",
    UseShellExecute = true,
    Verb = "open"
});
Run Code Online (Sandbox Code Playgroud)

此解决方案不适用于打开文件夹和选择项目,因为似乎没有动词.

  • 如果您使用此方法并且存在“C:\teste.exe”或“C:\teste.cmd”等文件夹,则资源管理器将打开到该其他文件夹,而不是您想要的文件夹。为了避免这种情况,您可以将 `Path.DirectorySeparatorChar` 附加到路径中。请参阅[VS本身如何犯同样的错误](https://developercommunity.visualstudio.com/content/problem/104305/.html)。 (3认同)

Scy*_*ion 16

如果要选择文件或文件夹,可以使用以下命令:

Process.Start("explorer.exe", "/select, c:\\teste");
Run Code Online (Sandbox Code Playgroud)

  • 要打开一个文件夹而不是选择它,只需将“ / select”更改为“ / open” (2认同)

Joe*_*orn 6

使用非转义字符串时不需要双反斜杠:

System.Diagnostics.Process.Start("explorer.exe",@"c:\teste");
Run Code Online (Sandbox Code Playgroud)


Kev*_*ity 5

您正在使用@符号,这消除了转义反斜杠的需要。

删除@或用\替换\\

  • 我会说……斑马。你的浏览器有问题,可能是 virii 或者其他什么...... (2认同)