Get Application Directory

This is a discussion on Get Application Directory within the C++ Programming forums, part of the General Programming Boards category; Does anyone here know how to get the Application Directory (regardless windows / linux environment) ?...

  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,318
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,544
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    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)

  12. #12
    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?

  13. #13
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,544
    > Because i want to read a configuration file, which is the same path as in the .exe
    Which might be fine if you're still on Win9x, but for XP and it's relatives, you should really be storing this information on a per user basis.

    Take a look in your C:\Documents and Settings\<user>\Application Data directory.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  14. #14
    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.

  15. #15
    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?

Page 1 of 2 12 LastLast
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, 01: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, 08:43 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21