views:

1425

answers:

6

On OS from win 2000 or later (any language) can I assume that this path will always exists? For example I know that on win xp in some languages the "Program Files" directory have a different name. So is it true for the System32 folder?

Thanks. Ohad.

+2  A: 

It might be safer to use the "windir" environment variable and then append the "System32" to the end of that path. Sometimes windows could be under a different folder or different drive so "windir" will tell you where it is.

As far as i know, the system32 folder should always exist under the windows folder.

Jarod Elliott
+4  A: 

Windows can be installed on a different harddrive and or in a different folder. Use the %windir% or %systemroot% environment variables to get you to the windows folder and append system32. Or use the %path% variable, it's usually the first entrance and the preferred method of searching for files such as dlls AFAIK. As per comments: don't rely too much on the system32 dir being the first item. I do think it's safe to assume it's in %path% somewhere though.

Jasper Bekkers
DON'T just take the first item in %path%. A host of program like to pre-append itself into the PATH, rather than post-append. System32 is about 9th on my LM PATH.
KTC
Definitely true, just assume it's in there somewhere, I'll edit :-)
Jasper Bekkers
+3  A: 

I would use the GetWindowsDirectory Win32 API to get the current Windows directory, append System32 to it an then check if it exists.

jussij
+16  A: 

No, you can't assume that.

Windows can be installed to a different path. One solution is to look for it by calling GetSystemDirectory (implemented as part of the Windows API).

vmarquez
+18  A: 

You definitely cannot assume that: Windows could be installed on a different drive letter, or in a different directory. On a previous work PC Windows was installed in D:\WINNT, for example.

The short answer is to use the API call GetSystemDirectory(), which will return the path you are after.

The longer answer is to ask: do you really need to know this? If you're using it to copy files into the Windows directory, I'd suggest you ask if you really want to do this. Copying into the Windows directory is not encouraged, as you can mess up other applications very easily. If you're using the path to find DLLs, why not just rely on the OS to find the appropriate one without giving a path? If you're digging into bits of the OS files, consider: is that going to work in future? In general it's better to not explicitly poke around in the Windows directory if you want your program to work on future Windows versions.

DavidK
I vote for this answer. My own has gotten more votes from others but definitively this one is far more complete and informative.Ohad42, please consider marking this one as the accepted answer.
vmarquez
Thanks David, Sorry that it took me such a long time too accept the answer.
Ohad Horesh
A: 

Just an FYI, but in a Terminal Server environment (ie, Citrix), GetWindowsDirectory() may return a unique path for a remote user.

link text

As more and more companies use virtualized desktops, developers need to keep this in mind.