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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1073999/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 07:36:25  来源:igfitidea点击:

How do I open the "My Documents" and "My Computer" folders from C#?

c#shortcut

提问by Suriyan Suresh

I have used two GUIDs to open the folders My Computerand My Documents.

我使用了两个 GUID 打开文件夹My ComputerMy Documents

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

But it opens Internet Explorer and then opens the folders My Computerand My Documents.

但它会打开 Internet Explorer,然后打开文件夹My ComputerMy Documents

采纳答案by Doctor Jones

Better still would be to skip explorerentirely and just "start" the GUIDs directly:

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

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

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

回答by heavyd

Try explorer.exe:

试试 explorer.exe:

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

回答by User

Have you tried:

你有没有尝试过:

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

?

?

回答by Doctor Jones

Using those hard coded Guid values doesn't look like the best way of achieving this.

使用这些硬编码的 Guid 值看起来并不是实现这一目标的最佳方式。

You could use the Environment.GetFolderPathfunction to get the path of any of the system special folders. It accepts an Environment.SpecialFolderenum.

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

This way it'd be more robust, because you wouldn't have any "magic" hardcoded values.

这样它会更健壮,因为你不会有任何“魔法”硬编码值。

Here's how you'd use it:

以下是您如何使用它:

//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);

Important note for Windows 7 users

Windows 7 用户的重要提示

It seems that trying to use this code to open My Computer on Windows 7 incorrectly results in the Libraries folder being opened instead. This is because the default behaviour of running explorer with an empty path has changed in Windows 7.

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

I've filed the following bug report over at connect, go and give it an upvote if you think that this is important!

我已经在 connect 提交了以下错误报告,如果您认为这很重要,请去给它点赞!

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

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

(Thanks to JeremyK in the comments for pointing this out)

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

回答by JOG

This does not work for my Vista:

这不适用于我的 Vista:

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

as Environment.SpecialFolder.MyComputer returns "" and Process.Start("explorer", "") opens My Documents.

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

The GUID seems to do it, though:

不过,GUID 似乎做到了:

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

回答by samdoss

Samdoss

萨姆多斯

Just enter the

只需输入

System.Diagnostics.Process.Start(directoryPath);

Its very easy. Try that.

它很容易。试试那个。

回答by anjaly grace

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

I know it looks doubtful but just run it. It'll work. This is the code for my computer. I don't know what it should be for My Documents.

我知道它看起来很可疑,但只需运行它。它会工作。这是我电脑的代码。我不知道My Documents应该是什么。

回答by Marco

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

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

I know it looks doubtful but just run it. It'll work. This is the code for my computer. I don't know what it should be for My Documents.

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

On Windows 7 this results in opening the folder from where your executable is running, i.e. the "current" folder.

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

回答by Sergei G

I had to open MyDocuments and based on comments above I narrowed down the solution to open Explorer without side effects:

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

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

I tested it on Windows Server 2008 R2.

我在 Windows Server 2008 R2 上对其进行了测试。