473,387 Members | 3,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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

Jun 1 '07 #1
9 4060
Application.StartupPath

Zytan

Jun 1 '07 #2

AppDomain.BaseDirectory

HTH,

Sam

------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

On Fri, 01 Jun 2007 18:10:28 -0000, Zytan <zy**********@gmail.com>
wrote:
>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
Jun 1 '07 #3
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

Jun 1 '07 #4

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

------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

On Fri, 01 Jun 2007 18:42:20 -0000, Zytan <zy**********@gmail.com>
wrote:
>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
Jun 1 '07 #5
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.Assembl y.GetExecutingAssembly.Location)

--
Peter Macej
Helixoft - http://www.helixoft.com
VSdocman - Commenter and generator of class documentation for C#, VB
..NET and ASP .NET code
Jun 2 '07 #6
Zytan wrote:
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.GetCommandLineAr gs()[0])

Arne
Jun 2 '07 #7
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

Jun 2 '07 #8
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.Assembl y.GetExecutingAssembly.Location)

Thanks, Peter.

Zytan

Jun 2 '07 #9
Zytan wrote:
>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.
If it is a console app then I absolutely think:

Path.GetDirectoryName(Environment.GetCommandLineAr gs()[0])

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

Arne
Jun 2 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Daniel | last post by:
How do you get the current directory of the actual .exe file (not the file name but the directory it is in) ? These do not work: System.IO.Directory.GetCurrentDirectory() - returns system...
4
by: Jon Rea | last post by:
Hi, Just a quickie ... I thought that Directory.GetCurrentGirectory(), unless the current directory had been changed by the program, would always point to the directory where the .exe for the...
3
by: Poppy | last post by:
Whenever I use "Directory.getcurrentdirectory" I always get "C:\WINDOWS\system32" despite the fact that the code is running from a subdirectory of wwwroot. Can someone help ? How can I get...
3
by: Progman | last post by:
I use Visual Studio 2005 When I run msgbox Curdir(), I get the visual studio directory. This problem seems to be only in the VB part. In the ASP, we use ~/etc... to access files. Why I...
4
by: Wayne | last post by:
about href links to other html pages in current directory: w3c seems to recommond using ./pagename and just pagename works too. Is there any actual difference in these two? I think no in...
3
by: Chris Mellon | last post by:
This appears to be a change in behavior from Python 2.4 to Python 2.5, which I can't find documented anywhere. It may be windows only, or related to Windows behavior. In 2.4, the current...
6
by: titan.nyquist | last post by:
The WebBrowser control won't load a css file written in the same directory as the program. If I put an absolute path to it, it will load it. Thus, the current directory of the WebBrowser control...
27
by: kvnsmnsn | last post by:
I've written a piece of code that interfaces with Postgres. It needs to write a Postgres table to disk, which it does with the <COPYcom- mand. That command requires the absolute file name of the...
8
by: Mateusz Viste | last post by:
Hi, I'm not sure if my question is really related to JavaScript, so please excuse me if that's not the case (and maybe you guys would have an idea what's the cause is and where could I ask)... ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.