我在系统会话下运行了win服务。我希望这个主服务控制(安装/删除)另一个winservice安装在另一个用户下(我们主服务有用户/密码)。 怎么做?
答案 0 :(得分:1)
你打算做什么听起来有点hackish,但如果你真的想这样做,那么你可以使用与Windows捆绑的sc.exe utility。您所要做的就是确保文件系统上有正确的服务文件(例如在%PROGRAMFILES%\[CompanyName]\[ServiceName]
下),然后使用Process.Start使用正确的命令行参数调用sc.exe。 / p>
要指定服务应在其下运行的帐户的名称和密码,请使用obj= <account name>
和password= <password>
选项。注意选项及其值之间的空格 - 如果没有该空格,命令将失败。
另一种选择是使用Process.Start()来调用installutil.exe(它是.Net框架的一部分)。一个简单的例子是:
var installutil = Environment.GetFolderPath(Environment.SpecialFolder.Windows)
+ "\\Microsoft.Net\\Framework\\v4.0.30319\\installutil.exe";
var arguments = string.Format( " /ServiceName=\"{0}\" /DisplayName=\"{1}\" \"{2}\" ",
serviceName,
displayName,
servicePath);
var psi = new ProcessStartInfo(installutil, arguments) {
CreateNoWindow = true,
RedirectStandardOutput = true,
RedirectStandardInput = false,
RedirectStandardError = false,
ErrorDialog = false,
UseShellExecute = false
};
var p = new Process { StartInfo = psi, EnableRaisingEvents = true };
p.Start();
p.WaitForExit();
答案 1 :(得分:0)
似乎我在主要的胜利服务中需要的所有呼叫:
//Installs and starts the service
ServiceInstaller.InstallAndStart("MyServiceName", "MyServiceDisplayName", "C:\PathToServiceFile.exe");
确保目标内部(我想控制/安装的那个)服务代码(ServiceInstaller)正确的安装程序:
public class MyServiceInstaller : Installer
{
private ServiceInstaller serviceInstaller;
private ServiceProcessInstaller processInstaller;
public MyServiceInstaller()
{
// instantiate installers for process and service
processInstaller = new ServiceProcessInstaller();
serviceInstaller = new ServiceInstaller();
// run under the system service account
processInstaller.Account = ServiceAccount.User;
processInstaller.Username = ".\\UserName";
processInstaller.Password = "password";
// service to start automatically.
serviceInstaller.StartType = ServiceStartMode.Automatic;
serviceInstaller.Description = "MyServiceDescription";
// name
string SvcName = "MyServiceNae";
// ServiceName must equal those on ServiceBase derived classes
serviceInstaller.ServiceName = SvcName;
// displayed in list
serviceInstaller.DisplayName = "My Service DisplayName ";
// add installers to collection
Installers.Add(serviceInstaller);
Installers.Add(processInstaller);
Committed += new InstallEventHandler((sender, args) =>
{
var controller = new System.ServiceProcess.ServiceController(SvcName);
controller.Start();
});
}
}
installandstart:
public static void InstallAndStart(
string serviceName,
string displayName,
string fileName,
string username=null,
string password = null)
{
IntPtr scm = OpenSCManager(null, null, ScmAccessRights.AllAccess);
try
{
IntPtr service = OpenService(
scm,
serviceName,
ServiceAccessRights.AllAccess);
if (service == IntPtr.Zero)
{
service = CreateService(
scm,
serviceName,
displayName,
ServiceAccessRights.AllAccess,
SERVICE_WIN32_OWN_PROCESS,
ServiceBootFlag.AutoStart,
ServiceError.Normal,
fileName,
null,
IntPtr.Zero,
null,
username,
password);
if (service == IntPtr.Zero)
throw new ApplicationException("Failed to install service.");
try
{
StartService(service, 0, 0);
}
finally
{
CloseServiceHandle(service);
}
}
}
finally
{
CloseServiceHandle(scm);
}
}