Find directory of .exe (it is not always the current directory)

Z

Zytan

If a program is run from a batch file, the current directory for the
program will be that of the batch file, not of the .exe. How can I
find the directory of the .exe? My program's files are in the same
directory as the .exe, and it assumes that it can find them by using
the current directory, which is not always the case.

thanks,
Zytan
 
Z

Zytan

Thanks, Sam.

Application.StartupPath = Gets the path for the executable file that
started the application, not including the executable name.

AppDomain.BaseDirectory = Gets the base directory that the assembly
resolver used to probe for assemblies.

It sounds like Application.StartupPath is more proper for what I want
(to know which directory the .exe resides in).

Zytan
 
S

Samuel R. Neff

You mentioned a console app, and Application is in the windows forms
dll. I wouldn't want to import a dll just to get the startup path.

While the descriptions differ, I'm not aware of what situations would
cause the values to be different.

Another possibility is Assembly.CodeBase but that needs some string
manipulation to get a proper directory path (it returns a file url).

Sam
 
P

Peter Macej

It sounds like Application.StartupPath is more proper for what I want

Just for information. The Application.StartupPath works fine for EXE.
But if your program is add-in, control or class library (which are DLLs
often located in different directory than main EXE program), you would
get EXE's path instead of your application path. The more general
approach which works with EXE and DLL is:

IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly.Location)
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Zytan said:
If a program is run from a batch file, the current directory for the
program will be that of the batch file, not of the .exe. How can I
find the directory of the .exe? My program's files are in the same
directory as the .exe, and it assumes that it can find them by using
the current directory, which is not always the case.

Path.GetDirectoryName(Environment.GetCommandLineArgs()[0])

Arne
 
Z

Zytan

You mentioned a console app, and Application is in the windows forms
dll. I wouldn't want to import a dll just to get the startup path.

Actually, I didn't say it was a console app, it is a Windows
application, it is being run from a batch file, which I guess is the
console, but the application being launched can be anything. If it
were a console, your suggestion is better, I agree.
While the descriptions differ, I'm not aware of what situations would
cause the values to be different.

Me, neither.
Another possibility is Assembly.CodeBase but that needs some string
manipulation to get a proper directory path (it returns a file url).

Ok, thanks.

Zytan
 
Z

Zytan

Just for information. The Application.StartupPath works fine for EXE.
But if your program is add-in, control or class library (which are DLLs
often located in different directory than main EXE program), you would
get EXE's path instead of your application path.

Ok, and the EXE's path is what I want.

But, I follow you, if it were a DLL, and I wanted to know the
directory of the DLL, and not the EXE, I would use your method:

IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly.Location)

Thanks, Peter.

Zytan
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Zytan said:
Actually, I didn't say it was a console app, it is a Windows
application, it is being run from a batch file, which I guess is the
console, but the application being launched can be anything. If it
were a console, your suggestion is better, I agree.

If it is a console app then I absolutely think:

Path.GetDirectoryName(Environment.GetCommandLineArgs()[0])

is best (it inly uses System and System.IO namespaces).

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top