Converting "User Shell Folders" registry value
The following registry key contains many system default folder locations.
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
The value for the path of the All Users desktop, which is found there, is as follows:
XP or earlier : [%ALLUSERSPROFILE%\Desktop]
Vista or later: [%PUBLIC%\Desktop]
Whereas the actual paths of the All User desktops, respectively, are as follows:
XP or earlier : "C:\Documents and Settings\All Users\Desktop"
Vista or later: "C:\Users\Public\Desktop"
Now, if you use copy and paste the above registry values in Windows Explorer and hit enter it takes you to the actual folders. For example, if you paste [%PUBLIC%\Desktop] in a Windows Explorer in Vista it takes you to ["C:\Users\Public\Desktop"].
My question is this; how do I reproduce this behavior from withing a C# program? To be more specific, if I retrieve the registry value [%PUBLIC%\Desktop] from withing a C# program, which I can do easily, how do I convert it to ["C:\Users\Public\Desktop"]? Obviously I'm not looking for a string replacement, I need to do wha开发者_StackOverflow中文版t Windows does.
Get the parh names by using GetFolderPath
Check out Environment.SpecialFolder
used like so:
string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
While you should be using GetFolderPath
as Mark says, to replicate the specific behaviour you're asking about, you can use the Environment.ExpandEnvironmentVariables() function to turn a string like "%ALLUSERSPROFILE%\Desktop" into "C:\Documents and Settings\All Users\Desktop" (or whatever)
Thanks guys for the replies. However,
Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
doesn't give me the result I need as it returns the Current User's desktop path.
Finally I figured out that I could use what codeka suggested. Also, when the registry key is retrieved it automatically calls that function.
By the way I hear that in .NET 4.0 they have added an enum to Environment.SpecialFolder so that Common Desktop can be retrieved.
精彩评论