在config中设置windows服务帐号

本文关键字:服务 帐号 windows 设置 config | 更新日期: 2023-09-27 18:04:20

我需要在配置文件中设置windows服务的serviceaccount。但是我无法访问安装程序中的配置值。我读了这个问题,但我不想使用安装参数。有什么办法可以做到吗?

编辑:

我也尝试了一些解决方案,但我无法在安装程序类中访问Configuration或ConfigurationManager…我是不是该加点参考资料?

在config中设置windows服务帐号

经过大量的研究,我找到了我自己的解决方案…

首先添加对System的引用。配置

,然后在安装程序类

中添加以下代码
Assembly service = Assembly.GetAssembly(typeof(ProjectInstaller));
string assemblyPath = service.Location;
Configuration config = ConfigurationManager.OpenExeConfiguration(assemblyPath);
KeyValueConfigurationCollection mySettings = config.AppSettings.Settings;
processInstaller.Account = (ServiceAccount)Enum.Parse(typeof(ServiceAccount), mySettings["Account"].Value);

我现在太开心了!!:)