如何在 .NET 控制台应用程序中获取应用程序的路径? [英] How can I get the application's path in a .NET console application?

查看:46
本文介绍了如何在 .NET 控制台应用程序中获取应用程序的路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.

推荐答案

System.Reflection.Assembly.GetExecutingAssembly().位置1

将其与 System.IO 结合起来.Path.GetDirectoryName 如果你想要的只是目录.

Combine that with System.IO.Path.GetDirectoryName if all you want is the directory.

1根据 Mr.Mindor 的评论:
System.Reflection.Assembly.GetExecutingAssembly().Location 返回正在执行的程序集当前所在的位置,这可能是也可能不是该程序集未执行时所在的位置.在卷影复制程序集的情况下,您将在临时目录中获得一个路径.System.Reflection.Assembly.GetExecutingAssembly().CodeBase 将返回程序集的永久"路径.

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 控制台应用程序中获取应用程序的路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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