|
-
May 18th, 2010, 02:25 AM
#1
How to convert Paths?
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 I 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 what Windows does.
-
May 18th, 2010, 05:03 AM
#2
Re: How to convert Paths?
try
Code:
string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
-
May 18th, 2010, 06:30 AM
#3
Re: How to convert Paths?
 Originally Posted by dannystommen
try
Code:
string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
That returns the current user's desktop folder path.
-
May 18th, 2010, 08:22 AM
#4
Re: How to convert Paths?
Code:
string path = Environment.GetFolderPath(Environment.SpecialFolder.CommonDesktopDirectory);
-
May 18th, 2010, 08:58 AM
#5
Re: How to convert Paths?
 Originally Posted by sachintha81
That returns the current user's desktop folder path.
It's not against the law to try it yourself. If you had tried some of the other values in the Environment.SpecialFolder enum you would have found it yourself.
It's not a bug, it's a feature!
-
May 18th, 2010, 09:01 PM
#6
Re: How to convert Paths?
 Originally Posted by dannystommen
Code:
string path = Environment.GetFolderPath(Environment.SpecialFolder.CommonDesktopDirectory);
Thanks Danny.
I'm using .NET 2.0 framework and in it there is no such enum as Environment.SpecialFolder.CommonDesktopDirectory. I hear they've added the feature in .NET 4.0 though.
-
May 18th, 2010, 09:03 PM
#7
Re: How to convert Paths?
 Originally Posted by foamy
It's not against the law to try it yourself. If you had tried some of the other values in the Environment.SpecialFolder enum you would have found it yourself.
I did try all the enum values before coming here actually. As stated in my previous reply, there is no Environment.SpecialFolder.CommonDesktopDirectory in .NET 2.0 unfortunately. I have to use either the registry key or environment variables instead.
-
May 18th, 2010, 09:10 PM
#8
Re: How to convert Paths?
 Originally Posted by sachintha81
That returns the current user's desktop folder path.
You could just look at the string returned. If the first folder is documents and settings then replace the user name with all users and if the first folder is users then replace the user name with public.
Always use [code][/code] tags when posting code.
-
May 18th, 2010, 09:17 PM
#9
Re: How to convert Paths?
 Originally Posted by DataMiser
You could just look at the string returned. If the first folder is documents and settings then replace the user name with all users and if the first folder is users then replace the user name with public.
Yes I could do that, but apparently there is a function that actually does pretty much the same.
Code:
Environment.ExpandEnvironmentVariables()
For example, if I use the following in Vista,
Code:
Environment.ExpandEnvironmentVariables(@"%PUBLIC%\Desktop")
then it returns;
"C:\Users\Public\Desktop"
And actually when I retrieve the reg key .NET does that for me.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|