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. How to get Windows Directory with C#

How to get Windows Directory with C#

Scheduled Pinned Locked Moved C#
csharphelptutorial
10 Posts 5 Posters 0 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.
  • M Offline
    M Offline
    Mmithat
    wrote on last edited by
    #1

    I was wondering If anyone had an experience with retrieving path of the Windows directory C# Unfortunately, Directory namespace has no class/member function to retrieve this information, And when I try to use this piece of code to import SDK function: [DllImport("kernel32")] private static extern uint GetWindowsDirectory(string lpBuffer, // buffer for Windows directory uint uSize // size of directory buffer ); But apperantly I couldnt make this work. Basically lpBuffer should be ref parameter in C#, but in decleration of this function is LPSTR in SDK, Any help will be appreciated, Thanks, ~Mithat

    L J 2 Replies Last reply
    0
    • M Mmithat

      I was wondering If anyone had an experience with retrieving path of the Windows directory C# Unfortunately, Directory namespace has no class/member function to retrieve this information, And when I try to use this piece of code to import SDK function: [DllImport("kernel32")] private static extern uint GetWindowsDirectory(string lpBuffer, // buffer for Windows directory uint uSize // size of directory buffer ); But apperantly I couldnt make this work. Basically lpBuffer should be ref parameter in C#, but in decleration of this function is LPSTR in SDK, Any help will be appreciated, Thanks, ~Mithat

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      System.Enviroment I rated this article 2 by mistake. It deserves more. I wanted to get to the second page... - vjedlicka 3:33 25 Nov '02

      M 1 Reply Last reply
      0
      • L leppie

        System.Enviroment I rated this article 2 by mistake. It deserves more. I wanted to get to the second page... - vjedlicka 3:33 25 Nov '02

        M Offline
        M Offline
        Mmithat
        wrote on last edited by
        #3

        I already checked the System.Environment And it does not have a memeber variable to rerieve the path for Windows directory . I want to retrieve c:\winNT not C:\winNT\system32 (which you can retrieve with Environment.SystemDirectory()) or any of those: Program Files, Programs, System, or Startup directory (which you can retrieve with Environment.GetFolderPath()) is there any method in System.Environment that rerieves Windows Directory path ? Thanks ~Mithat

        L 2 Replies Last reply
        0
        • M Mmithat

          I already checked the System.Environment And it does not have a memeber variable to rerieve the path for Windows directory . I want to retrieve c:\winNT not C:\winNT\system32 (which you can retrieve with Environment.SystemDirectory()) or any of those: Program Files, Programs, System, or Startup directory (which you can retrieve with Environment.GetFolderPath()) is there any method in System.Environment that rerieves Windows Directory path ? Thanks ~Mithat

          L Offline
          L Offline
          leppie
          wrote on last edited by
          #4

          Mmithat wrote: not C:\winNT\system32 just remove the \system32 bit or \system in win9x.... X| I rated this article 2 by mistake. It deserves more. I wanted to get to the second page... - vjedlicka 3:33 25 Nov '02

          M 1 Reply Last reply
          0
          • M Mmithat

            I already checked the System.Environment And it does not have a memeber variable to rerieve the path for Windows directory . I want to retrieve c:\winNT not C:\winNT\system32 (which you can retrieve with Environment.SystemDirectory()) or any of those: Program Files, Programs, System, or Startup directory (which you can retrieve with Environment.GetFolderPath()) is there any method in System.Environment that rerieves Windows Directory path ? Thanks ~Mithat

            L Offline
            L Offline
            leppie
            wrote on last edited by
            #5

            Or you can try: Environment.GetEnvironmentVariable("windir"); I havent tried it, but it should work :) I rated this article 2 by mistake. It deserves more. I wanted to get to the second page... - vjedlicka 3:33 25 Nov '02

            M 1 Reply Last reply
            0
            • L leppie

              Or you can try: Environment.GetEnvironmentVariable("windir"); I havent tried it, but it should work :) I rated this article 2 by mistake. It deserves more. I wanted to get to the second page... - vjedlicka 3:33 25 Nov '02

              M Offline
              M Offline
              Mmithat
              wrote on last edited by
              #6

              I like that better. Thanks =) ~Mithat

              1 Reply Last reply
              0
              • L leppie

                Mmithat wrote: not C:\winNT\system32 just remove the \system32 bit or \system in win9x.... X| I rated this article 2 by mistake. It deserves more. I wanted to get to the second page... - vjedlicka 3:33 25 Nov '02

                M Offline
                M Offline
                Mmithat
                wrote on last edited by
                #7

                this is how i retrieve it right now, csTempDirectory = Environment.SystemDirectory; //csTempDirectory = Environment.GetEnvironmentVariable("windir"); m_csWindowsDirectory = Directory.GetParent(csTempDirectory).ToString(); But I dont want to rely on those kinda stuff sometimes system does not let u get those paths. and it may vary . in my orginal message, there should be a way to import this function (getwindowsdirectory()) from SDK But i am just not comfortable with the variables in import-declaration-functions. ~Mithat

                1 Reply Last reply
                0
                • M Mmithat

                  I was wondering If anyone had an experience with retrieving path of the Windows directory C# Unfortunately, Directory namespace has no class/member function to retrieve this information, And when I try to use this piece of code to import SDK function: [DllImport("kernel32")] private static extern uint GetWindowsDirectory(string lpBuffer, // buffer for Windows directory uint uSize // size of directory buffer ); But apperantly I couldnt make this work. Basically lpBuffer should be ref parameter in C#, but in decleration of this function is LPSTR in SDK, Any help will be appreciated, Thanks, ~Mithat

                  J Offline
                  J Offline
                  James T Johnson
                  wrote on last edited by
                  #8

                  [DllImport("kernel32.dll", CharSet=CharSet.Auto)]
                  private static extern uint GetWindowsDirectory(
                  [MarshalAs(UnmanagedType.LPTStr)]
                  System.Text.StringBuilder lpBuffer, // buffer for Windows directory
                  uint uSize // size of directory buffer
                  );

                  // 255 == buffer size
                  System.Text.StringBuilder buffer = new System.Text.StringBuilder(255);
                  GetWindowsDirectory( buffer, (uint) buffer.MaxCapacity );

                  Works here. I think as a general rule, when you need to pass in a buffer to receive text you use a StringBuilder object. [edit]Looking at MSDN for the GetWindowsDirectory function it is supposed to accept a TCHAR, so the MarshalAs and DllImport attributes should be changed accordingly as above.[/edit] James "It is self repeating, of unknown pattern" Data - Star Trek: The Next Generation

                  U 1 Reply Last reply
                  0
                  • J James T Johnson

                    [DllImport("kernel32.dll", CharSet=CharSet.Auto)]
                    private static extern uint GetWindowsDirectory(
                    [MarshalAs(UnmanagedType.LPTStr)]
                    System.Text.StringBuilder lpBuffer, // buffer for Windows directory
                    uint uSize // size of directory buffer
                    );

                    // 255 == buffer size
                    System.Text.StringBuilder buffer = new System.Text.StringBuilder(255);
                    GetWindowsDirectory( buffer, (uint) buffer.MaxCapacity );

                    Works here. I think as a general rule, when you need to pass in a buffer to receive text you use a StringBuilder object. [edit]Looking at MSDN for the GetWindowsDirectory function it is supposed to accept a TCHAR, so the MarshalAs and DllImport attributes should be changed accordingly as above.[/edit] James "It is self repeating, of unknown pattern" Data - Star Trek: The Next Generation

                    U Offline
                    U Offline
                    User 138817
                    wrote on last edited by
                    #9

                    [DllImport("kernel32.dll", CharSet=CharSet.Auto)]private static extern uint GetWindowsDirectory( [MarshalAs(UnmanagedType.LPTStr)] System.Text.StringBuilder lpBuffer, // buffer for Windows directory uint uSize // size of directory buffer); Really great language C#. Fast, simple and very comfortable...

                    S 1 Reply Last reply
                    0
                    • U User 138817

                      [DllImport("kernel32.dll", CharSet=CharSet.Auto)]private static extern uint GetWindowsDirectory( [MarshalAs(UnmanagedType.LPTStr)] System.Text.StringBuilder lpBuffer, // buffer for Windows directory uint uSize // size of directory buffer); Really great language C#. Fast, simple and very comfortable...

                      S Offline
                      S Offline
                      Stephane Rodriguez
                      wrote on last edited by
                      #10

                      Better get used to it. ;)

                      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