How can I get the application's path in a .NET

2018-12-31 02:07发布

How do I find the application's path in a console application?

In Windows Forms, I can use Application.StartupPath to find the current path, but this doesn't seem to be available in a console application.

26条回答
与风俱净
2楼-- · 2018-12-31 03:06

For Console Applications, you can try this:

System.IO.Directory.GetCurrentDirectory();

Output (on my local machine):

c:\users\xxxxxxx\documents\visual studio 2012\Projects\ImageHandler\GetDir\bin\Debug

Or you can try (there's an additional backslash in the end):

AppDomain.CurrentDomain.BaseDirectory

Output:

c:\users\xxxxxxx\documents\visual studio 2012\Projects\ImageHandler\GetDir\bin\Debug\

查看更多
美炸的是我
3楼-- · 2018-12-31 03:06

I have used

System.AppDomain.CurrentDomain.BaseDirectory

when I want to find a path relative to an applications folder. This works for both ASP.Net and winform applications. It also does not require any reference to System.Web assemblies.

查看更多
登录 后发表回答