windir

aewarnick

Senior Contributor
Joined
Jan 29, 2003
Messages
1,031
How can I reference the windows directory for any os in C#? You can do it in batch files and should be able to in C#.
 
Last edited:
You can use Environment.SystemDirectory to retreive the path to the system directory, and use string manipulation or the methods of the Path class to get just the Windows directory from that.

As far as I know there's no framework way to get just the Windows directory, but I'd like to be proved wrong.
 
I hope there is a method to get just the windows dir.
If I just used the method above would it be the same for ever windows os?
Ex: mine is System32.
 
Use DirectoryInfo

System (or System32) is always a direct sub-folder of Windows (or Win2k/NT or whatever) so use the following code:
Code:
VB:
Public Function GetWinDir() as String
     'Create a directory info structure of C:\Win2k\System32 or C:\Windows\system, etc.
     Dim di as New DirectoryInfo(Environment.SystemDirectory)
     'Return the full path of its parent
     Return di.Parent.FullName
End Function
C#:
public String GetWinDir()
{
     //Create a directory info structure of C:\Win2k\System32 or C:\Windows\system, etc.
     DirectoryInfo di = new DirectoryInfo(Environment.SystemDirectory)
     // Return the full path of its parent
     return di.Parent.FullName
}
No matter the Windows version this should return the windows dir
 
Back
Top