Can anyone tell me how i can obtain the "program files" directory?
For example i want to make a program to obtain the c:\Program Files \ .....
or f:\Program Files \ .....
How do i know which directory is the primary to obtain ?
Can anyone tell me how i can obtain the "program files" directory?
For example i want to make a program to obtain the c:\Program Files \ .....
or f:\Program Files \ .....
How do i know which directory is the primary to obtain ?
The primary one will be the one found within the returned string from issuing a path command without any other parameters.
What I'd use would be something like this:
system ("path") and direct the output to a file. Then read the file. In a console window, enter help path for more details on path or pipes and redirecting to a file.
If you have programs which operate from another drive's Program Files directory, you may need to have your program choose the lowest one, or the one which has the Windows/system directory listed, etc.
path var can contain a lot of other things
set ProgramFiles should show the desired path
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
It's not located in PATH necessarily. It does have it's own special variable, though.
At the command line you can do this:
To read the variable value from a C program: http://msdn2.microsoft.com/en-us/library/ms683188.aspxCode:echo %programfiles%
Code:#include <stdio.h> #include <Windows.h> int main(void) { char szBuffer[BUFSIZ]; if(GetEnvironmentVariable("programfiles",szBuffer,sizeof(szBuffer))) { printf("Program files dir: %s\n",szBuffer); } else printf("Unable to find program files dir...\n"); return 0; }
Is there any reason to use that rather then getenv()?
There is no greater sign that a computing technology is worthless than the association of the word "solution" with it.
To be completely honest, no.
For whatever reason, since he was asking about program files, I went with the Windows API right away without thinking of getenv().
Both work:
Actually, there might be one small reason, which is trivial. I believe getenv() is not thread-safe, and your string may get overwritten by other calls to it. GetEnvironmentVariable() is thread-safe, I believe. This is mainly conjecture, though, and I'm probably wrong on this. Even if I'm right, the portable aspect of getenv() is probably better than the Windows API provided you don't get caught in any of the traps.Code:#include <stdio.h> #include <stdlib.h> #include <Windows.h> int main(void) { char szBuffer[BUFSIZ]; char *szEnv; if(GetEnvironmentVariable("programfiles",szBuffer,sizeof(szBuffer))) { printf("Program files dir: %s\n",szBuffer); } else printf("Unable to find program files dir...\n"); szEnv = getenv("programfiles"); if(szEnv) { printf("Program files dir: %s\n",szBuffer); } else printf("Unable to find program files dir...\n"); return 0; }
uh wait a minute, isn't that a bit dishonest? szEnv is relatively ignored here, being assigned unused stuff.Code:int main(void) { char szBuffer[BUFSIZ]; char *szEnv; if(GetEnvironmentVariable("programfiles",szBuffer,sizeof(szBuffer))) { printf("Program files dir: %s\n",szBuffer); } else printf("Unable to find program files dir...\n"); szEnv = getenv("programfiles"); if(szEnv) { printf("Program files dir: %s\n",szBuffer); } else printf("Unable to find program files dir...\n");
Dishonest? No. Stupid? Yes. Absolute mistake when I copied the printf()'s. It does work when szEnv is used in place of szBuffer in the second situation.
Mentioning dishonesty was a little harsh for a mistake so obvious.
Perhaps I made a poor word choice, but I don't have much of a vocabulary to be honest ( since google implemented define I can at least be assured I'm using proper words... ). Despite an honest flub like that one I found it rather dishonest because it didn't support the claim at first. If your point is that getenv returns the same information as platform specific code, then it is reasonable to expect the code demonstrates the fact. I'd rather not have you embarrass yourself like I do.
I was hoping it would be corrected, and I succeeded, so... hooray.
Adjusted and color coded.Code:#include <stdio.h> #include <stdlib.h> #include <Windows.h> int main(void) { char szBuffer[BUFSIZ]; char *szEnv; if(GetEnvironmentVariable("programfiles",szBuffer,sizeof(szBuffer))) { printf("Program files dir: %s\n",szBuffer); } else printf("Unable to find program files dir...\n"); szEnv = getenv("programfiles"); if(szEnv) { printf("Program files dir: %s\n",szEnv); } else printf("Unable to find program files dir...\n"); return 0; }
Well you still make quite a citizenOriginally Posted by citizen