如何从C#打开"我的文档"和"我的电脑"文件夹?

Sur*_*esh 3 c# shortcut

我使用了两个GUID打开文件夹" 我的电脑"和" 我的文档".

Process.Start("iexplore.exe", "::{20d04fe0-3aea-1069-a2d8-08002b30309d}");
Process.Start("iexplore.exe", "::{450d8fba-ad25-11d0-98a8-0800361b1103}");
Run Code Online (Sandbox Code Playgroud)

但它会打开Internet Explorer,然后打开" 我的电脑"和" 我的文档 "文件夹.

Rei*_*ica 38

使用那些硬编码的Guid值看起来不是实现此目的的最佳方式.

您可以使用Environment.GetFolderPath函数来获取任何系统特殊文件夹的路径.它接受Environment.SpecialFolder枚举.

这样它会更健壮,因为你不会有任何"神奇的"硬编码值.

这是你如何使用它:

//get the folder paths
string myComputerPath = Environment.GetFolderPath(Environment.SpecialFolder.MyComputer);
string myDocumentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
//open explorer and point it at the paths
System.Diagnostics.Process.Start("explorer", myComputerPath);
System.Diagnostics.Process.Start("explorer", myDocumentsPath);
Run Code Online (Sandbox Code Playgroud)

Windows 7用户的重要说明

似乎尝试使用此代码在Windows 7上打开我的电脑不正确会导致打开Libraries文件夹.这是因为在Windows 7中,使用空路径运行资源管理器的默认行为已更改.

我已经在connect上提交了以下错误报告,如果您认为这很重要,那么请给它一个upvote!

https://connect.microsoft.com/VisualStudio/feedback/details/757291/environment-getfolderpath-not-working-correctly-in-windows-7#details

(感谢JeremyK在评论中指出这一点)


Use*_*ser 9

你有没有尝试过:

Process.Start("explorer.exe", "::{20d04fe0-3aea-1069-a2d8-08002b30309d}");
Process.Start("explorer.exe", "::{450d8fba-ad25-11d0-98a8-0800361b1103}");
Run Code Online (Sandbox Code Playgroud)


hea*_*vyd 6

尝试explorer.exe:

Process.Start("explorer.exe", "::{20d04fe0-3aea-1069-a2d8-08002b30309d}");
Process.Start("explorer.exe", "::{450d8fba-ad25-11d0-98a8-0800361b1103}");
Run Code Online (Sandbox Code Playgroud)


小智 5

更好的是explorer完全跳过并直接"启动"GUID:

Process.Start("::{20d04fe0-3aea-1069-a2d8-08002b30309d}");...

  • 硬核!这将为初级开发人员提供很多乐趣,他们将在5年后通过这种代码进行调试:) (4认同)