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

时间:2009-07-02 12:08:28

标签: c# shortcut

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

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

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

9 个答案:

答案 0 :(得分: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);

Windows 7用户的重要提示

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

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

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

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

答案 1 :(得分:9)

你试过了吗?

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

答案 2 :(得分:6)

尝试使用explorer.exe:

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

答案 3 :(得分:5)

最好还是完全跳过explorer并直接“启动”GUID:

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

答案 4 :(得分:2)

这对我的Vista无效:

string myComputerPath = Environment.GetFolderPath(Environment.SpecialFolder.MyComputer);
System.Diagnostics.Process.Start("explorer", myComputerPath);

作为Environment.SpecialFolder.MyComputer返回“”并且Process.Start(“explorer”,“”)打开我的文档。

GUID似乎也是这样做的:

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

答案 5 :(得分:1)

System.Diagnostics.Process.Start("...");

我知道它看起来很令人怀疑,但只是运行它。它会工作的。这是我的电脑的代码。我不知道我的文档应该是什么。

答案 6 :(得分:0)

  

System.Diagnostics.Process.Start("...");

我知道它看起来很令人怀疑,但只是运行它。它会工作的。这是我的电脑的代码。我不知道我的文件应该是什么。

在Windows 7上,这会导致打开运行可执行文件的文件夹,即“当前”文件夹。

答案 7 :(得分:0)

我必须打开MyDocuments并根据上面的评论我缩小了打开资源管理器的解决方案,没有副作用:

Process.Start("::{450d8fba-ad25-11d0-98a8-0800361b1103}");

我在Windows Server 2008 R2上测试过它。

答案 8 :(得分:-2)

Samdoss

只需输入

即可
System.Diagnostics.Process.Start(directoryPath);

非常容易。试试吧。

相关问题