Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
300 views
in Technique[技术] by (71.8m points)

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

I have used two GUIDs to open the folders My Computer and My 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 Computer and My Documents.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

You could use the Environment.GetFolderPath function to get the path of any of the system special folders. It accepts an Environment.SpecialFolder enum.

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

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.

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

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...