如何获得的控制台应用程序执行的目录 [英] How to get execution directory of console application

查看:149
本文介绍了如何获得的控制台应用程序执行的目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着用下面的code,以获得控制台应用程序的目录,

  Assembly.GetExecutingAssembly()位置。
 

但是这一次给了我所在的装配所在。这可能是从哪里执行的应用不同。

我的控制台应用程序解析不带参数的日志。它必须去日志/ 文件夹中的可执行文件的文件夹内,或者如果我给它一个路径日志/ 资讯分析它。

解决方案

使用 Environment.CurrentDirectory

  

获取或设置当前工作目录的完全限定路径。
  (MSDN <一个href="http://msdn.microsoft.com/en-us/library/system.environment.currentdirectory%28v=vs.90%29">Environment.CurrentDirectory物业)

 字符串logsDirectory = Path.Combine(Environment.CurrentDirectory,日志);
 

如果您的应用程序正在运行的 C:\富\酒吧 logsDirectory 将指向的 C:\富\酒吧\日志的。

I tried to get the directory of the console application using the below code,

Assembly.GetExecutingAssembly().Location

but this one gives me where the assemble resides. This may be different from where I executed the application.

My console application parses logs with no parameters. It must go to the logs/ folder inside of the executable's folder or if I give it a path to logs/ it parses it.

解决方案

Use Environment.CurrentDirectory.

Gets or sets the fully qualified path of the current working directory.
(MSDN Environment.CurrentDirectory Property)

string logsDirectory = Path.Combine(Environment.CurrentDirectory, "logs");

If your application is running in c:\Foo\Bar logsDirectory will point to c:\Foo\Bar\logs.

这篇关于如何获得的控制台应用程序执行的目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆