Determining the Windows folder location properly

LHandy

Well-known member
Joined
May 27, 2005
Messages
50
Programming Experience
5-10
Resolved: Determining the Windows folder location properly

I'm aware of Environment.SpecialFolders but the Windows folder is not available there. I've been using what seems to me to be the sloppy way of finding the Windows directory:

VB.NET:
Environ("windir")

That doesn't seem like the right way to do it in .NET. There must be a better way. Anybody?

Also, I don't want to have to parse the system32 folder out of SpecialFolders.System so please don't suggest that :p

Thanks
 
Last edited:
It's more about locating the windows directory to further include any desired sub folders. Such as \temp, \drivers, \drivers\etc
 
As Jim said it doesn't make much sense to me too.
However, you could first find the Sysytem Directory and then just refer to Windows folder as it's always located in the root of the system's drive (almost always named C:\)
Also, note that you could use a "GetSystemDirectory" API function for the purpose

Cheers ;)

added: or just Desperate try to getting over:

VB.NET:
Dim str As String = Environment.GetFolderPath(Environment.SpecialFolder.System)

Dim windowsFolder As String = str.Substring(0, str.Length - 9) 

MessageBox.Show(windowsFolder)

and finally :D take a look at this article: http://www.freevbcode.com/ShowCode.asp?ID=4571
 
Last edited:
Yeah but I don't want to have to parse out the system folder, that's the thing. I don't want to hard code anything into it as I want it to work on any windows system.

It is possible to install Windows on a drive other than C: (such as installing Windows 98, then dual booting a Windows XP partition, it can happen). It's possible to install it in another folder other than "Windows" also (like when installing over an old version of Windows rather than doing a fresh install, the installer asks if you want to install to another folder).

I don't want to take any chances by hard coding anything. My program needs to access files in the Windows folder as well as some files in sub-directories of it. And parsing the system or system32 folder out of the system path just isn't neat enough for me. (Yeah, I'm very anal about being neat :D)

Environment.GetEnvironmentVariable("windir") works for me. But this is one example of why it should be in special folders. It makes sense to me. Some programs may need to access files or folders within the Windows directory -- assuming otherwise just creates headaches.
 
Don't work too hard! You could set a Directory object to the System32 folder, then just get its parent directory, voila, you'd have the Windows folder. Also look at the System.IO.Path and properties/methods to do the same.
 
Back
Top