如何在控制台应用程序中找到应用程序的路径?

Windows Forms中,我可以使用Application.StartupPath查找当前路径,但这似乎在控制台应用程序中不可用.

推荐答案

System.Reflection.Assembly.GetExecutingAssembly().Location1

如果你想要的只是目录,就把它和System.IO.Path.GetDirectoryName结合起来.

1As per Mr.Mindor's comment:
System.Reflection.Assembly.GetExecutingAssembly().Location returns where the executing assembly is currently located, which may or may not be where the assembly is located when not executing. In the case of shadow copying assemblies, you will get a path in a temp directory. System.Reflection.Assembly.GetExecutingAssembly().CodeBase will return the 'permanent' path of the assembly.

.net相关问答推荐

为什么Linq中的运算符逻辑不匹配结果,当值为0或在VB. NET中没有

查询 MongoDb 中嵌入式文档中的一个字段,该字段抛出调用运算符的左侧必须是对持久属性的直接访问

在 web api 控制器(.net 核心)中使用 async/await 或任务

如何中止任务,如中止线程(Thread.Abort 方法)?

比较 C# 中的字符串和对象

具有透明背景且包含不透明控件的 WPF 窗口

使用 IIS Express 托管网站(临时)

使用 EPPlus 时如何设置列类型

添加新实体标量时实体框架 4 映射片段错误

将 Topshelf 应用程序安装为 Windows 服务

如何将 UI Dispatcher 传递给 ViewModel

日期时间是什么意思?在 C# 中是什么意思?

修剪数组中的所有字符串

在 C# 中使用委托

关闭 Visual Studio 中所有选项卡但当前选项卡的键盘快捷键?

为什么 !0 是 Microsoft 中间语言 (MSIL) 中的一种类型?

根据条件从列表中删除项目

如何在不使用 3rd 方库的情况下登录 C#?

在 IIS 中访问 .svc 文件时出现 HTTP 404

.NET 桌面应用程序中的 Settings.settings 与 app.config