Does anyone here know how to get the Application Directory (regardless windows / linux environment) ?
Printable View
Does anyone here know how to get the Application Directory (regardless windows / linux environment) ?
what do you mean the application directory and how do you want to use it
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.
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: %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;
}
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 ?
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.Quote:
Originally Posted by cppnewbie81
just curious: for what reason do you want to know this directory?
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
Or even just myconfig.ini ... (Relative to the working directory)Code:blah = fopen("./myconfig.ini", "rt");
> 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.
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.
What if you want to make a portable app?
What's exactly the reason I wrote:)Quote:
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.