find the startup path of a console application

  • Thread starter Thread starter Eranga
  • Start date Start date
E

Eranga

In c# windows applicatiopns the Application.StartupPath can be used to
find the path for the executable file. How can the same be found for a
console application?(we can't use application because it inherits from
the System.Windows.Forms namespace)
Thanks in advance.
 
try this instead

string appPath =
System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);

cheers
 
Eranga said:
In c# windows applicatiopns the Application.StartupPath can be used to
find the path for the executable file. How can the same be found for a
console application?(we can't use application because it inherits from
the System.Windows.Forms namespace)
Thanks in advance.

how about this:

using System.IO;

DirectoryInfo di = new DirectoryInfo(".");
Console.WriteLine(di.FullName);
 
That is equivalent with Directory.GetCurrentDirectory, which does not
always equal to Application.StartupPath (the path of the executable
that started the app).

The best way, I guess, to get Appication.StartupPath is:
Process.GetCurrentProcess().MainModule.FileName

Note that this returns the full path to the executable file while
Application.StartupPath does not contain the file name. To get the path
without file name use Path.GetDirectoryName function.
 
Truong Hong Thi said:
That is equivalent with Directory.GetCurrentDirectory, which does not
always equal to Application.StartupPath (the path of the executable
that started the app).

Thanks,

I always start my console Apps from the same directory as the app, so I have never come across this.

Bill
 

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

Back
Top