我有一个服务,安装后将 Myapp.exe.config 文件(基于 app.config 文件生成)放入安装目录。在安装过程中,我需要此文件中的详细信息来配置安装。特别是我需要调整服务的 ServiceProcessInstaller 实例的帐户/用户名/密码字段,以便在它运行时以特定用户身份运行。
但是,在安装过程中,安装程序尚未设置我的注册表设置,也没有将我的 Myapp.exe.config 复制到安装目录......所以我无法提取这些值,这意味着我的服务无法正确安装用户。我现在能做到的唯一方法是将用户/传递值硬编码到 ProjectInstaller 类中,但我不能让自己这样做。这是错误的。
是否有关于如何以特定用户身份安装服务以及如何在安装过程中访问这些凭据的 Windows 安装最佳实践。
目前我正在尝试类似的东西:
namespace MyService
{
[RunInstaller(true)]
public partial class ProjectInstaller : System.Configuration.Install.Installer
{
public ProjectInstaller()
{
InitializeComponent();
//Set the user credentials.
//NOTE: Eventually this needs to be updated to pull these values out of a
// conf file. The problem now is that the location of the conf file
// is tied to a registry entry for the location of the service which
// may or may not exist when this block is executed.
/* This is the only way I can get it to work, but this is
* too horrible to ever actually do it this way:
serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.User;
serviceProcessInstaller1.Username = "DOMAIN\\user";
serviceProcessInstaller1.Password = "password";
*/
// Try to pull the service's registry values to know where it installed:
RegistryKey keyService = Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\services\\MyService");
string path = ((string)keyService.GetValue("ImagePath"));
path = path.Substring(1, path.LastIndexOf('\\'));
string user = someValueFromFileIOontheAbovePath1,
pass = someValueFromFileIOontheAbovePath2;
serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.User;
serviceProcessInstaller1.Username = user;
serviceProcessInstaller1.Password = pass;
//Doesn't work because install isn't complete and there aren't reg settings
// yet, and even if I had the path the .config file is not written yet so
// there's no file to parse.
}
}
}
一些包含这些参数的 .config 文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="User" value="DOMAIN\bob"/>
<add key="Password" value="secret"/>
</appSettings>
</configuration>
如何在安装过程中提取这些用户/密码值而不将它们硬编码到二进制文件中?
谢谢。