How to get Windows Directory with C#
-
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 -
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 -
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
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 withEnvironment.GetFolderPath()
) is there any method in System.Environment that rerieves Windows Directory path ? Thanks ~Mithat -
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 withEnvironment.GetFolderPath()
) is there any method in System.Environment that rerieves Windows Directory path ? Thanks ~Mithat -
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 withEnvironment.GetFolderPath()
) is there any method in System.Environment that rerieves Windows Directory path ? Thanks ~Mithat -
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 -
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
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 -
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[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 -
[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[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...
-
[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...
Better get used to it. ;)