本文整理汇总了C#中System.ServiceProcess.ServiceInstaller.Commit方法的典型用法代码示例。如果您正苦于以下问题:C# ServiceInstaller.Commit方法的具体用法?C# ServiceInstaller.Commit怎么用?C# ServiceInstaller.Commit使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.ServiceProcess.ServiceInstaller
的用法示例。
在下文中一共展示了ServiceInstaller.Commit方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: InstallService
/// <summary>
/// Install an executable as a service.
/// </summary>
/// <param name="assemblyPath">The path to the executable.</param>
/// <param name="serviceName">The name of the service.</param>
/// <param name="displayName">THe display name of the service.</param>
/// <param name="description">The description of the service.</param>
/// <param name="startType">The startup type.</param>
/// <param name="userName">The username to run as.</param>
/// <param name="password">The password of the user.</param>
/// <param name="dependancies"></param>
public static void InstallService(string assemblyPath, string serviceName, string displayName, string description,
ServiceStartMode startType, string userName = "", string password = "", IEnumerable<string> dependancies = null)
{
using (var procesServiceInstaller = new ServiceProcessInstaller())
{
if (string.IsNullOrEmpty(userName))
{
procesServiceInstaller.Account = ServiceAccount.LocalSystem;
}
else
{
procesServiceInstaller.Account = ServiceAccount.User;
procesServiceInstaller.Username = userName;
procesServiceInstaller.Password = password;
}
using (var installer = new ServiceInstaller())
{
var cmdline = new[] { string.Format("/assemblypath={0}", assemblyPath) };
var context = new InstallContext(string.Empty, cmdline);
installer.Context = context;
installer.DisplayName = displayName;
installer.Description = description;
installer.ServiceName = serviceName;
installer.StartType = startType;
installer.Parent = procesServiceInstaller;
if (dependancies != null)
{
installer.ServicesDependedOn = dependancies.ToArray();
}
IDictionary state = new Hashtable();
try
{
installer.Install(state);
installer.Commit(state);
}
catch (Exception ex)
{
installer.Rollback(state);
throw new Exception("Failed to install the service.", ex);
}
}
}
}
示例2: Install
static void Install(bool install, InstallerOptions options)
{
var spi = new ServiceProcessInstaller();
var si = new ServiceInstaller();
spi.Account = ServiceAccount.NetworkService;
if (options != null && options.IsUser)
{
spi.Account = ServiceAccount.User;
if (options.UserName != null)
{
spi.Username = options.UserName;
}
if (options.Password != null)
{
spi.Password = options.Password;
}
}
si.StartType = ServiceStartMode.Automatic;
si.ServiceName = "CloudBackup";
si.DisplayName = "Cloud Backup Service";
si.Description = "Schedules, run and manage cloud backup";
si.Parent = spi;
string path = Assembly.GetEntryAssembly().Location;
Console.WriteLine("Location : " + path);
var ic = new InstallContext();
ic.Parameters.Add("assemblypath", path);
si.Context = ic;
spi.Context = ic;
IDictionary rb = install ? new Hashtable() : null;
try
{
Console.WriteLine("Starting Default Installation");
if (install)
{
si.Install(rb);
}
else
{
si.Uninstall(rb);
}
}
catch (Exception ex)
{
log.Fatal(ex);
if (rb != null)
{
Console.WriteLine("Rollback Default Installation");
IDictionary rbc = rb;
rb = null;
si.Rollback(rbc);
}
}
finally
{
if (rb != null)
{
Console.WriteLine("Commit Default Installation");
si.Commit(rb);
}
}
}