Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Does a console application know where it lives? 1

Status
Not open for further replies.

alexjones

Programmer
Jul 27, 2001
132
0
0
US
I'd like my console application to read data from a text file which resides in the same directory as the application. Console doesn't seem to have an Application class like that found in Windows.Forms. Is there something else I can use to determine my application's path?
 
Here are two method:

1) Environment.GetCommandLineArgs() returns an array containing all of the command line arguments. The first argument in this array is the canonical path to the application. No matter how the application is started ("..\myApp", "debug\myApp", etc.) the first element in the array will have the full path to your application, including the drive letter.

e.g.

string appStartupFolder = new System.IO.FileInfo(Environment.GetCommandLineArgs()[0]).DirectoryName

2) System.Reflection.Assembly.GetEntryAssembly() returns the path (including the filename, as with GetCommandLineArgs).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top