什么是应用程序的等价物.Windows Service中的ExecutablePath
本文关键字:Service 中的 ExecutablePath Windows 等价物 应用程序 什么 | 更新日期: 2023-09-27 18:18:58
在windows窗体应用程序中,我使用application。进入App.config.的可执行路径,
我需要得到应用程序配置在Windows服务..
几个选项:
System.Reflection.Assembly.GetExecutingAssembly().Location
用于当前程序集。或者,您可以从类型派生它:
System.Reflection.Assembly.GetAssembly(typeof(MyAssemblyType)).Location
然后(在任何一个)你可以使用Path.GetDirectoryName
获得它的起源文件夹(假设你的app.config是在同一目录)。
看看AppDomain。BaseDirectory属性
获取程序集解析程序用来探测的基目录组件。
最常见的用法类似于
var baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
可能的选项有:
AppDomain.CurrentDomain.BaseDirectory
或
Assembly.Location
或
Assembly.GetExecutingAssembly().CodeBase
您可以使用ConfigurationManager
读取App.config:
-
在你的项目中添加
System.Configuration
的引用 -
像这样使用
ConfigurationManager
读取App.config:ConfigurationManager.AppSettings["KeySample"]
在配置文件中,你可以像这样添加你的密钥:
<configuration> <appSettings> <add key="KeySample" value="SampleValue"/> </appSettings> </configuration>
所以你不需要得到路径