Thread: Obtaining path

  1. #1
    Registered User ch4's Avatar
    Join Date
    Jan 2007
    Posts
    154

    Obtaining path

    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 ?

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    It's not located in PATH necessarily. It does have it's own special variable, though.

    At the command line you can do this:

    Code:
    echo %programfiles%
    To read the variable value from a C program: http://msdn2.microsoft.com/en-us/library/ms683188.aspx

    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;
    }

  5. #5
    Massively Single Player AverageSoftware's Avatar
    Join Date
    May 2007
    Location
    Buffalo, NY
    Posts
    141
    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.

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    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:

    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: &#37;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;
    }
    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.

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    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");
    uh wait a minute, isn't that a bit dishonest? szEnv is relatively ignored here, being assigned unused stuff.

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    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.

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612


    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.

  10. #10
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    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;
    }
    Adjusted and color coded.

  11. #11
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by citizen
    Perhaps I made a poor word choice, but I don't have much of a vocabulary to be honest
    Well you still make quite a citizen

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Shortest Path Maze Solver (Breadth Search Help)
    By Raskalnikov in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 07:41 PM
  2. Can't figure out what keeps hanging up my program
    By shays in forum C Programming
    Replies: 7
    Last Post: 11-12-2007, 02:59 PM
  3. Shortest path problem
    By Digitalxero in forum C++ Programming
    Replies: 0
    Last Post: 10-25-2005, 05:32 PM
  4. Path Finding Using Adjacency List.
    By Geolingo in forum C++ Programming
    Replies: 7
    Last Post: 05-16-2005, 02:34 PM
  5. linked list recursive function spaghetti
    By ... in forum C++ Programming
    Replies: 4
    Last Post: 09-02-2003, 02:53 PM