Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Get name of system folder [using C#]

Get name of system folder [using C#]

Scheduled Pinned Locked Moved C#
csharpcomworkspace
15 Posts 4 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S shivamkalra

    Hi, I've a university issued laptop with windows 7 on it. When I click on "windows" button it show my name as one of the system folder, but there is no path for it. It is above the "My Document" folder, in fact everyone in the university has same folder with their name [obviously]. I want to make an program that could extract the name of this folder when its it run on a particular system. And computer name, account name or name of Environment.getFolderName(SpecialFolder.Personal) all are different from this folder. I do not know where is this folder even stored. CHECK THIS LINK FOR AN IMAGE http://dl.dropbox.com/u/10120092/Untitled.png[^] SHIVAM :)

    D Offline
    D Offline
    DaveyM69
    wrote on last edited by
    #3

    As an alternative to Luc's suggestion you can combine the SystemDrive and HOMEPATH environment variables something like this:

    public static string GetUserFolder()
    {
    return
    Environment.GetEnvironmentVariable("SystemDrive") +
    Environment.GetEnvironmentVariable("HOMEPATH");
    }

    Edit: Just tested from explorer and using Process.Start and it appears to work with only the HOMEPATH variable too

    Dave
    Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
    BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

    1 Reply Last reply
    0
    • L Luc Pattyn

      There isn't a direct way, this however should give what you want:

      string s=Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
      s=s.SubString(0, s.Length-8); // drop \Desktop

      :)

      Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

      Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

      D Offline
      D Offline
      DaveAuld
      wrote on last edited by
      #4

      Shivam (/Luc); You can access this folder with the SpecialFolder UserProfile; String s = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); :)

      Dave Find Me On: Web|Facebook|Twitter|LinkedIn


      Folding Stats: Team CodeProject

      D 1 Reply Last reply
      0
      • D DaveAuld

        Shivam (/Luc); You can access this folder with the SpecialFolder UserProfile; String s = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); :)

        Dave Find Me On: Web|Facebook|Twitter|LinkedIn


        Folding Stats: Team CodeProject

        D Offline
        D Offline
        DaveyM69
        wrote on last edited by
        #5

        Only if using .NET framework V4

        Dave
        Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
        BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

        D 1 Reply Last reply
        0
        • D DaveyM69

          Only if using .NET framework V4

          Dave
          Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
          BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

          D Offline
          D Offline
          DaveAuld
          wrote on last edited by
          #6

          Well the OP did not make reference to any .Net version, i don't know what he is using :) [at least he knows now it exists in later frameworks!)

          Dave Find Me On: Web|Facebook|Twitter|LinkedIn


          Folding Stats: Team CodeProject

          D 1 Reply Last reply
          0
          • S shivamkalra

            Hi, I've a university issued laptop with windows 7 on it. When I click on "windows" button it show my name as one of the system folder, but there is no path for it. It is above the "My Document" folder, in fact everyone in the university has same folder with their name [obviously]. I want to make an program that could extract the name of this folder when its it run on a particular system. And computer name, account name or name of Environment.getFolderName(SpecialFolder.Personal) all are different from this folder. I do not know where is this folder even stored. CHECK THIS LINK FOR AN IMAGE http://dl.dropbox.com/u/10120092/Untitled.png[^] SHIVAM :)

            D Offline
            D Offline
            DaveyM69
            wrote on last edited by
            #7

            Having seen Dave's answer that it is available in v4, I've had a look at how it's done using reflector and these two functions combined perform the same task and will work with earlier .NET versions so long as it's run on a platform that supports it - if not, a PlatformNotSupportedException will be raised.

            public const int UserProfile = 40;

            [DllImport("shfolder.dll", CharSet = CharSet.Auto)]
            private static extern int SHGetFolderPath(IntPtr hwndOwner, int nFolder, IntPtr hToken, int dwFlags, StringBuilder lpszPath);

            public static string GetUserPath()
            {
            StringBuilder resultBuilder = new StringBuilder(260);
            int num = SHGetFolderPath(IntPtr.Zero, UserProfile, IntPtr.Zero, 0, resultBuilder);
            if (num == -2146233031)
            throw new PlatformNotSupportedException();
            return resultBuilder.ToString();
            }

            Dave
            Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
            BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

            1 Reply Last reply
            0
            • D DaveAuld

              Well the OP did not make reference to any .Net version, i don't know what he is using :) [at least he knows now it exists in later frameworks!)

              Dave Find Me On: Web|Facebook|Twitter|LinkedIn


              Folding Stats: Team CodeProject

              D Offline
              D Offline
              DaveyM69
              wrote on last edited by
              #8

              True. I've had a look in reflector and it's easy enough to do this in previous versions - see my other answer[^] below

              Dave
              Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
              BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

              1 Reply Last reply
              0
              • L Luc Pattyn

                There isn't a direct way, this however should give what you want:

                string s=Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                s=s.SubString(0, s.Length-8); // drop \Desktop

                :)

                Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                S Offline
                S Offline
                shivamkalra
                wrote on last edited by
                #9

                Hi, nothing seems to work. I tried your way, it only shows some other folder on my desktop.

                L 1 Reply Last reply
                0
                • S shivamkalra

                  Hi, nothing seems to work. I tried your way, it only shows some other folder on my desktop.

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #10

                  try to be specific. What have you tried and what was the outcome? What operating system, which .NET version, what exactly do you get, what exactly would you like to get. It works for me! :)

                  Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                  Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                  S 1 Reply Last reply
                  0
                  • L Luc Pattyn

                    try to be specific. What have you tried and what was the outcome? What operating system, which .NET version, what exactly do you get, what exactly would you like to get. It works for me! :)

                    Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                    Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                    S Offline
                    S Offline
                    shivamkalra
                    wrote on last edited by
                    #11

                    I tried your code. I get "C:\Users\100xxxxxx\Desktop" where 100xxxxxx is my student ID. I'm using .Net 3.5 but I do have .Net 4 with visual studios 10. Not why it is not working for me.. S

                    L 1 Reply Last reply
                    0
                    • S shivamkalra

                      I tried your code. I get "C:\Users\100xxxxxx\Desktop" where 100xxxxxx is my student ID. I'm using .Net 3.5 but I do have .Net 4 with visual studios 10. Not why it is not working for me.. S

                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #12

                      And is there a folder somewhere that holds your actual name instead of your student ID? All GetFolderPath() results are actual folder names. When a user account gets created, the user name displayed is also used as the folder name; it is however possible to change one without changing the other, as explained e.g. here[^]. I guess that is what happened to the student accounts: first create them numerically, then modify their display names. The display name of the current user should be available through Environment.UserName :)

                      Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                      Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                      S 1 Reply Last reply
                      0
                      • L Luc Pattyn

                        And is there a folder somewhere that holds your actual name instead of your student ID? All GetFolderPath() results are actual folder names. When a user account gets created, the user name displayed is also used as the folder name; it is however possible to change one without changing the other, as explained e.g. here[^]. I guess that is what happened to the student accounts: first create them numerically, then modify their display names. The display name of the current user should be available through Environment.UserName :)

                        Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                        Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                        S Offline
                        S Offline
                        shivamkalra
                        wrote on last edited by
                        #13

                        Very strange but I do not see this folder anywhere on my computer. It shows when I press windows button or when I'm choosing or opening some file. I tried Environment.UserName but it is showing my student ID [LOL]. Anyways thanks, I checked your link, probably this what happening in my case too. DAM IT, but there has to be some folder which starts with my name. Shivam

                        L 1 Reply Last reply
                        0
                        • S shivamkalra

                          Very strange but I do not see this folder anywhere on my computer. It shows when I press windows button or when I'm choosing or opening some file. I tried Environment.UserName but it is showing my student ID [LOL]. Anyways thanks, I checked your link, probably this what happening in my case too. DAM IT, but there has to be some folder which starts with my name. Shivam

                          L Offline
                          L Offline
                          Luc Pattyn
                          wrote on last edited by
                          #14

                          You're welcome. BTW: Most of the username/foldername magic is hidden in the registry, as the article explains. :)

                          Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                          Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                          S 1 Reply Last reply
                          0
                          • L Luc Pattyn

                            You're welcome. BTW: Most of the username/foldername magic is hidden in the registry, as the article explains. :)

                            Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                            Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                            S Offline
                            S Offline
                            shivamkalra
                            wrote on last edited by
                            #15

                            YUPPII. I solved the problem. I searched my name inside the registry and found that everyone is school has same registry key to that their name as registry value..so in my program I'm reading the registry value from this registry key..and it is working fine..thanks everyone again.. SHIVAM :)

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups