CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jul 2008
    Posts
    48

    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.

  2. #2
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: How to convert Paths?

    try
    Code:
    string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

  3. #3
    Join Date
    Jul 2008
    Posts
    48

    Re: How to convert Paths?

    Quote Originally Posted by dannystommen View Post
    try
    Code:
    string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
    That returns the current user's desktop folder path.

  4. #4
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: How to convert Paths?

    Code:
    string path = Environment.GetFolderPath(Environment.SpecialFolder.CommonDesktopDirectory);

  5. #5
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    Re: How to convert Paths?

    Quote Originally Posted by sachintha81 View Post
    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!

  6. #6
    Join Date
    Jul 2008
    Posts
    48

    Re: How to convert Paths?

    Quote Originally Posted by dannystommen View Post
    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.

  7. #7
    Join Date
    Jul 2008
    Posts
    48

    Re: How to convert Paths?

    Quote Originally Posted by foamy View Post
    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.

  8. #8
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: How to convert Paths?

    Quote Originally Posted by sachintha81 View Post
    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.

  9. #9
    Join Date
    Jul 2008
    Posts
    48

    Re: How to convert Paths?

    Quote Originally Posted by DataMiser View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured