我怎样才能获得应用程序的一个.NET控制台应用程序路径? [英] How can I get the application's path in a .NET console application?

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

问题描述

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

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

Windows窗体,我可以使用 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.

推荐答案

<一个href=\"http://msdn.microsoft.com/en-us/library/system.reflection.assembly.getexecutingassembly.aspx\"><$c$c>System.Reflection.Assembly.GetExecutingAssembly().<$c$c>Location1

再加上<一个href=\"http://msdn.microsoft.com/en-us/library/system.io.path.getdirectoryname.aspx\"><$c$c>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()。位置返回将执行的程序集当前所在,这可能是也可能不是所在的组件位于时不执行。在影子复制组件的情况下,你会得到一个临时目录的路径。 <一href=\"https://msdn.microsoft.com/en-us/library/system.reflection.assembly.$c$cbase(v=vs.110).aspx\"><$c$c>System.Reflection.Assembly.GetExecutingAssembly().$c$cBase将返回组件的永久性的路径。

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.

还要注意,作为<一个href=\"http://stackoverflow.com/questions/837488/how-can-i-get-the-applications-path-in-a-net-console-application#comment51636955_837501\">bitbonk观察, GetExecutingAssembly 返回包含code装配的当前正在执行的,这不一定是控制台 .exe文件组装。它可以是已经从一个完全不同的位置加载由控制台组件的组件。对于入门组装,需要<一个href=\"https://msdn.microsoft.com/en-us/library/system.reflection.assembly.getentryassembly.aspx\"><$c$c>GetEntryAssembly:

Note also that as bitbonk observes, GetExecutingAssembly returns the assembly that contains the code that is currently executing, which may not necessarily be the console .exe assembly. It may be an assembly that has been loaded by the console assembly from a totally different location. For the entry assembly, you need GetEntryAssembly:

获取默认应用程序域中的进程可执行文件。在其他应用领域,这是一个由 AppDomain.ExecuteAssembly 执行的第一个可执行文件。

Gets the process executable in the default application domain. In other application domains, this is the first executable that was executed by AppDomain.ExecuteAssembly.

bitbonk还指出, codeBase的当组件在GAC可能没有设置,所以<一个href=\"https://msdn.microsoft.com/en-us/library/system.appdomain.basedirectory(v=vs.110).aspx\"><$c$c>AppDomain.CurrentDomain.BaseDirectory也应考虑。

bitbonk also notes that CodeBase might not be set when the assembly is in the GAC, so AppDomain.CurrentDomain.BaseDirectory should also be considered.

这篇关于我怎样才能获得应用程序的一个.NET控制台应用程序路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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