在C#中获取应用程序路径 [英] Get application path in c#
本文介绍了在C#中获取应用程序路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在安装后通过C#代码获取应用程序路径.
我该怎么办?我应该使用注册表项吗?
Hi, I want to get my application path after I install it, by C# code.
How can I get that? Should I use registry key?
推荐答案
使用 ^ ]
对于当前程序集,您可以使用:
For the current assembly you can use:
System.Reflection.Assembly.GetExecutingAssembly().Location
请参阅: Assembly.Location属性 [
See: Assembly.Location Property[^].
Depending on the situation (your application structure) you may want to find out the location of the entry assembly. In that case use:
System.Reflection.Assembly.GetEntryAssembly().Location
这是最通用的方式:
Here is the most universal way:
string exePath =
System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetEntryAssembly().Location);
其他方法可能需要不使用的库(例如System.Windows.Forms
),或者取决于代码的托管方式(例如,使用Main
的参数不可靠).
Other methods either require a library which you don''t use (such as System.Windows.Forms
) or depends on how the code is hosted (for example, using parameters of Main
is not reliable).
这篇关于在C#中获取应用程序路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文