Thread: Get Application Directory

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    10

    Get Application Directory

    Does anyone here know how to get the Application Directory (regardless windows / linux environment) ?

  2. #2
    Registered User
    Join Date
    Jun 2007
    Posts
    24
    what do you mean the application directory and how do you want to use it

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    10
    Quote Originally Posted by tbca View Post
    what do you mean the application directory and how do you want to use it

    Hi, sorry if it's been unclear ,,
    what i mean is is there any function for me to Get the current .exe directory path ? For example if my .exe store in C:\Program Files\MyProgram, call this function will return me this path

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I believe there is no standard solution for this. It is possible that a library like boost filesystem has a cross-platform way of getting the directory. Otherwise, you'll have to have separate code for Windows and Linux.

  5. #5
    Registered User Frobozz's Avatar
    Join Date
    Dec 2002
    Posts
    546
    If you use GCC, then this will work but Visual Studio is missing dirent. Some more information on dirent is at the wikipedia.

    Code:
    /*
     * Lists the contents of the current directory
     */
    #include <dirent.h>
    #include <stdio.h>
    
    int main()
    {
    	DIR           *directory;
    	struct dirent *entry;
    	unsigned char  running = 1;
    	
    	directory = opendir(".");
    	if (directory == NULL)
    	{
    		printf("Unable to list current directory.\n");
    	}
    	else
    	{
    		printf("Contents of: &#37;s\n\n", directory->dd_name);
    		
    		while (running)
    		{
    			entry = readdir(directory);
    			if (entry != 0)
    				printf("%s\n", entry->d_name);
    			else
    				running = 0;
    		}
    	}
    	
    	closedir(directory);
    	return 0;
    }

  6. #6
    Registered User
    Join Date
    Jul 2007
    Posts
    10

    Unhappy

    Ok for now let's forget about the Linux part (Linux is for future consideration) ..
    Im using Microsoft Visual Studio .Net ... If there's no standard function for me to get the application directory path, does it mean that i have to "FORCE" the end-users install my program in certain directory by hardcoding the path ?

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User Frobozz's Avatar
    Join Date
    Dec 2002
    Posts
    546
    Quote Originally Posted by cppnewbie81
    Ok for now let's forget about the Linux part
    That was wrote and tested under Windows XP using MinGW for a compiler just in case you weren't aware that GCC is available for more than just Linux. Also if you want a Visual Studio version of the library, check here.

  9. #9
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    just curious: for what reason do you want to know this directory?

  10. #10
    Registered User
    Join Date
    Jul 2007
    Posts
    10
    Quote Originally Posted by pheres View Post
    just curious: for what reason do you want to know this directory?
    Because i want to read a configuration file, which is the same path as in the .exe ..

  11. #11
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    Quote Originally Posted by cppnewbie81 View Post
    Because i want to read a configuration file, which is the same path as in the .exe ..
    That is bad style in most cases. All modern OS provide better places to store such files. On windows it's the registry or the directory salem mentioned.
    Reasons: If you read/write to the application install directory users are only able to run your software using an administrative account which has serious security implications.
    Also a standard backup will not look into application install directories.

  12. #12
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Quote Originally Posted by pheres View Post
    That is bad style in most cases. All modern OS provide better places to store such files. On windows it's the registry or the directory salem mentioned.
    What if you want to make a portable app?

  13. #13
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    Quote Originally Posted by Darryl View Post
    What if you want to make a portable app?
    What's exactly the reason I wrote
    That is bad style in most cases.


    In this case backup and access rights are not of relevance, because the user account usally has write permission to an usb stick or whatever he plugs in and such media are normally not touched by backup scripts.

    But If you make you app "portable" you should think about how this can annoy users who install to hard disc the normal way. Maybe one can work around this by runtime checking somehow if the app is started in normal or portable mode.
    Last edited by pheres; 07-11-2007 at 06:03 AM.

  14. #14
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Quote Originally Posted by pheres View Post
    just curious: for what reason do you want to know this directory?
    Err... For relative addressing?

  15. #15
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Have you read Salem's post yet?!?!?

    You can also use GetWorkingDirectory() or whatever it's called.

    You could also use a relative path, ie

    Code:
    blah = fopen("./myconfig.ini", "rt");
    Or even just myconfig.ini ... (Relative to the working directory)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  2. Couple errors please help :-D
    By JJJIrish05 in forum C Programming
    Replies: 9
    Last Post: 03-06-2008, 02:54 AM
  3. Replies: 6
    Last Post: 07-30-2003, 03:08 AM
  4. Application Directory?
    By Nick Howes in forum Windows Programming
    Replies: 12
    Last Post: 07-02-2003, 04:25 PM
  5. Directory reading trouble
    By samGwilliam in forum Linux Programming
    Replies: 0
    Last Post: 03-10-2002, 09:43 AM