Showing posts with label application path. Show all posts
Showing posts with label application path. Show all posts

Tuesday, March 18, 2008

[C++] How to retrieve Application Path

Hi There,
welcome back to "the Best Resource on the web for Application Path retrieval" (Jack Jones - Collective Development); after .NET Application Path and JAVA Application Path we present a quick reference about how to retrieve Application Path using C++ and raw WinAPIs.

There are two common ways to retrieve App Path in C++ in a windows environment:

1) GetCurrentDirectory - this is probably the most common way to do it but it has a Drawback: the current directory path is not always the directory from where your assembly is being executed; You can alway change your current directory with a call to SetCurrentDirectory Indeed. This function fills a buffer with the current directory path (without filename), and returns the path size (termination character excluded); in case of errors it returns 0, In case the buffer is too short it returns the buffer required size:

#define PATH_LENGTH 1023

char buffer[PATH_LENGTH];
CString AppPathNoFileName;
int rv = GetCurrentDirectory(PATH_LENGTH,buffer);

if((!rv) || (rv > PATH_LENGTH))
{
//something wrong!
}
else
{
AppPathNoFileName = buffer;
}


Ref: GetCurrentDirectory

2) GetModuleFileName - this is safer if you wanna be sure to get the directory from which the current assembly is being executed. With this method you get not only the path but the file name as well, so you might wanna parse the resulting string in order to cut the filename if you don't need it. If the buffer is too short the path gets trucated. The function returns the size of path (number of characters- excluding end of string character) or 0 if an error occurs:


HINSTANCE hInst = AfxGetInstanceHandle();
char _buffer[PATH_LENGTH];
CString AppPath, AppPathNoName;

if(!GetModuleFileName(hInst, _buffer, PATH_LENGTH))
{
//Troubles!
}
else
{
AppPath = _buffer;
AppPathNoName = AppPath.Left(ApplicationPath_.Find("\\[appname].[appext]", 0));
}


Ref: GetModuleFileName

That's all; Stay out of troubles.

Thursday, February 14, 2008

[JAVA] How to Retrieve Application Path

This brief article will explain in a few lines how to retrieve the application path in a JAVA enviroment, both the official way and the butchers' way (just as we like!).

(1) The first way calls the System.getProperty static method passing the user.dir value:

String dir = System.getProperty("user.dir");

This way is simple but I found that sometimes (I'm not sure if Sun has fixed it) the returned directory could be slightly different from the one in which the application resides: this is because some times it gives out the directory in which the application has been started and not the one where your classes are.

This happened to me using Eclipse Plugins: usign several plugins, I needed a common directory to retrieve some XML resources, but the working directory returned was the one in which eclipse was running and of course not the same of the plugins one.

I fixed it using a ResourceBundle class. Actually that wasn't a bug, because most of libraries needed to run my plugins were inside Eclipse directory.

(2)And now the butchers' method! This method creates a fictitious file and retrieves its absolute path: I found that this method can overcome the problems using user.dir system property:

java.io.File currentDir = new java.io.File("");
String dir = currentDir.getAbsolutePath();

Generally to retrieve your resources that are in a class directory (and maybe not in the working directory) you won't need the entire path, because we can use this method:
InputStream in = MyClass.class.getResourceAsStream("MyImage.gif");

So my advice is using paths of external directories and storing them into configuration files (such as .properties files) or calling resources without using absolute paths; moreover don't try to modify user.dir system property because it is set when your application starts and if you change it you don't know how will be the behaviour of all other classes.

Stay tuned!

Thursday, January 31, 2008

[.NET] How to Retrieve Application Path - quick reference

Problem: You need to retrieve the application path but You don't remember (or you never knew) how to do it.

Solution: there are at least 3 well-known ways of ways to do this in .NET, let's see them in detail:

(1) the System.IO.Path way - more code but you always get what you're looking for; let's look at simple sample:

You start by using System.Reflection to get the application path comprehensive of the file name, either this way:

string pathWithFileName;
pathWithFileName=System.Reflection.Assembly.GetExecutingAssembly().Location;


Or this Way:

pathWithFileName=System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase.Replace("file:/", "");


Then you pass the pathWithFileName string to System.IO.Path.GetDirectoryName:


string pathWithoutFileName;
pathWithoutFileName = System.IO.Path.GetDirectoryName(code_Base);


(2) the Sytem.IO.Directory way - it's easier to type (since you'll be copy-pasting like a good butcher I don't see the catch) and to get but there is a severe drawback:

pathWithoutFileName=System.IO.Directory.GetCurrentDirectory();

What's the severe drawback? Well if you previously changed the Current Directory using the SetCurrentDirectoy method you'll be getting that result instead of the the directory from where your assembly is being executed. If you're the only one working on your app, maybe that's ok, but if we're talking about huge banks of code... up to you, butcher.

(3) the System.Windows.Forms.Application way - if you're using windows forms you can use the StartupPath property:

pathWithoutFileName=System.IO.Forms.Application.StartupPath;

Enough - have a good butcherin' day; You bet I will.