![](/img/trans.png)
[英]Atlassian account logon through WebBrowser control in WinForms application
[英]Installshield winforms service creation with specific logon user account
使用此C#代码,我可以让我的Windows服务用户
System.Management.SelectQuery sQuery = new System.Management.SelectQuery(string.Format("select Name,startname from Win32_Service where name ='MyWindowsServices' "));
using (System.Management.ManagementObjectSearcher mgmtSearcher = new System.Management.ManagementObjectSearcher(sQuery))
{
foreach (System.Management.ManagementObject service1 in mgmtSearcher.Get())
{
//If not local system , then associated with different user
if(service1["startname"].ToString().ToLower()!="localsystem")
{
service1["startname"].ToString()).ToString()
}
}
}
这是创建Windows服务安装的installshield脚本。 即。 如果系统已经安装了我的Windows服务,那么当他们再次重新安装时,我需要确保我需要指出运行Windows服务的用户。 (如果它的本地系统应该指向本地系统,如果它的特定用户(例如sharpeye500)具有一些密码,那么它应该指向sharpeye500(用户)并弹出(例如设置服务登录名)以显示密码)。
Set objCreate = objWMIService.Get("Win32_BaseService")
Get Window Service
Set colListOfServices = objWMIService.ExecQuery _
("Select * from Win32_Service Where Name = 'MyWindowsServices'")
'First Stop and Delete service and then install
For Each objService in colListOfServices
//我得到与服务关联的用户名
startName=objService.StartName
objService.StopService()
Next
'I have delete service logic & then i install Window Service again
objCreate.Create "MyWindowsServices" ,"My Windows Service Description" ,
"c:\test\" & "MyWindowsServices.exe", OWN_PROCESS, NORMAL_ERROR_CONTROL,
"Automatic", NOT_INTERACTIVE, startName, ""
但是,当我以上述语法传递startName时,安装失败,但如果传递Null,它将使用localsystem用户帐户创建Windows服务安装。
如何获取与Windows服务关联的用户帐户,并通过installshield脚本将其分配回安装程序?
System.Management.SelectQuery sQuery = new System.Management.SelectQuery(string.Format("select startname from Win32_Service where name ='MyWindowsService' "));
using (System.Management.ManagementObjectSearcher mgmtSearcher = new System.Management.ManagementObjectSearcher(sQuery))
{
foreach (System.Management.ManagementObject service in mgmtSearcher.Get())
{
if (service["startname"].ToString().ToLower() != "localsystem" || service["startname"].ToString().ToLower() != "localservice")
{
this.ServiceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.User;
this.ServiceProcessInstaller1.Username = service["startname"].ToString();
}
else
{
this.ServiceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
this.ServiceProcessInstaller1.Password = null;
this.ServiceProcessInstaller1.Username = null;
}
}
}
我有这个逻辑,我需要做的就是将其放在installscript(在vb中)中,但是我正在努力如何在installscript中使用ManagementObject。
这种方法行不通。 首先,虽然您可以轻松获得服务帐户的用户名,但无法调用获取帐户密码的方法(明显的安全限制)。
删除该服务将很顺利,但是Create方法需要除LocalSystem以外的其他任何帐户的用户名和密码(再次出于安全性考虑),您将无法完全拥有该帐户。 对于除LocalSystem以外的任何帐户,这都是一个问题。
为什么必须删除并重新安装服务? 您能否简单地停止该服务并更新该服务调用的基础可执行文件/ dll? 您甚至可以使用API函数更改服务的命令行...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.