Path to executable

  • Thread starter Thread starter FireStarter
  • Start date Start date
F

FireStarter

Guys,

How can I get the path to the current executable at run time? I think the VB6
equivalent is App.Path.



Thanks for your help.

FireStarter
 
Hi,

You can use System.Reflection.Assembly.GetExecutingAssembly.Location.

Hope this helps

Chris Taylor
 
Maybe I should also explain what I want to do.
I have a .dll and I want to build an XML configuration file for it. At run time
I will go to the configuration file and get some settings out of it (database
connection string).
I'm using an XmlDocument and try loading the config file with:

XmlDocument appConfig = new XmlDocument();
appConfig.Load("filename.config");

The problem is, when I run the the program it is looking in winnt\system32 for
filename.config.
I have tried to use System.IO.Directory.GetCurrentDirectory(), but that returns
winnt\system32, also (?!).

What gives? how can i rewrite line 2 of the code above so that it reads a file
in the same folder with the .dll?


I hope this makes sense.

Thanks. FireStarter.
 
Chris,

Thanks for the prompt response.
When I use the dll in an ASP.NET application, that returns:

"c:\winnt\microsoft.net\framework\v1.1.4322\temporary asp.net
files\ecommerce\c080a7de\d2993907\assembly\dl2\2eaf87ff\
50714524_a378c301\ecommbizobjects.dll\"

Ugh! Of course, my configuration file is not found :)
 
Ah, ASP.NET. Take a look at HttpServerUtility.MapPath or Server.MapPath.
Of course, it only gives you the virtual path where the ASP.NET application
is running. From there, you might be able to get the location of your config
file.

Cheers!
 
Hi,

Beware that Application.ExecutablePath returns the location of the
executable file that started your application, most times it is the exe
however if your exe is started by another application you will get the path
to the starting exe.

Cheers

Chris Taylor
 
Hi,

Sorry, I did not realize you were referring to a ASP.NET application.
I do not work much with ASP.NET, so I would have to go with what Jop Pacual
said in his post.

Regards

Chris Taylor
 
Hi Chris,

IF you are dealing with an ASP.NET app then it's a different history, as is
the aspnet_wp.exe the one that is executing and he is the one that load your
dlls ( that are locate in the /bin directory ) , what you can get is the
physical directory of your application, you do this by using
HttpServerUtility.MapPath this is the only way I know to get the location,
after that you know that the dll is located under the /bin directory.
now the tricky part is that you want ot know that from inside the dll ,
usually HttpServerUtility is only accesible from the asp.net application, I
would advice you agains including code to use HttpServerUtility.MapPath
inside the dll, cause if other day you want to use it from a windows app
then you are going to get errors.

My suggested solution:
declare a static public property like:
public static string ConfigFileLocation="";
declare a flag as :
private bool isLoadedConfig = false;
then in the method or methods that you need info from this file you check
first for this flag:
if ( ! isLoadedConfig )
LoadConfig();

this method check to see if ConfigFileLocation is emtpy if so then you use
System.Reflection.Assembly.GetExecutingAssembly.Location , otherwise you use
the string provided in the ConfigFileLocation variable.

Of course you would have to check if the file exist , otherwise you throw
an exception

Something like this:

void LoadConfig()
{
string path = (ConfigFileLocation=="") ?
System.Reflection.Assembly.GetExecutingAssembly.Location :
ConfigFileLocation;

if ( ! File.Exist( path) )
throw new Exception ("File Not Found");

}


Anyway this is a possible solution, you have to check the code above, cause
I just write it down in OE :)

Hope this help,
 
Back
Top