Aug 1, 2011

How to get the Windows Directory using VB.NET or C#

While programming, you may need to find the windows path for various purposes. Many programmers assume windows path will be C:\\Winnt if it is a Windows 2000 machine and C:\\Windows if it is a Windows 2003 system.

It need not be true. Also, the windows path varies by operating system. So, it is a good idea to programmatically find the windows path instead of assuming it.

Here is the code to find the windows root path:

Environment.GetEnvironmentVariable("SystemRoot")


Alternatively, you can access another environment variable to find the windows directory.


Environment.GetEnvironmentVariable("windir")


You can use the above approach to find various other special folders.

No comments:

Post a Comment