本文整理汇总了C#中System.ServiceProcess.ServiceInstaller.Install方法的典型用法代码示例。如果您正苦于以下问题:C# ServiceInstaller.Install方法的具体用法?C# ServiceInstaller.Install怎么用?C# ServiceInstaller.Install使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.ServiceProcess.ServiceInstaller
的用法示例。
在下文中一共展示了ServiceInstaller.Install方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Install
public virtual void Install(string serviceName)
{
Logger.Info("Installing service '{0}'", serviceName);
var installer = new ServiceProcessInstaller
{
Account = ServiceAccount.LocalSystem
};
var serviceInstaller = new ServiceInstaller();
String[] cmdline = { @"/assemblypath=" + Process.GetCurrentProcess().MainModule.FileName };
var context = new InstallContext("service_install.log", cmdline);
serviceInstaller.Context = context;
serviceInstaller.DisplayName = serviceName;
serviceInstaller.ServiceName = serviceName;
serviceInstaller.Description = "NzbDrone Application Server";
serviceInstaller.StartType = ServiceStartMode.Automatic;
serviceInstaller.Parent = installer;
serviceInstaller.Install(new ListDictionary());
Logger.Info("Service Has installed successfully.");
}
示例2: 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);
}
}
}
}
示例3: Install
public void Install()
{
if (ServiceExists())
{
logger.Warn("The service is already installed!");
}
else
{
var installer = new ServiceProcessInstaller
{
Account = ServiceAccount.LocalSystem
};
var serviceInstaller = new ServiceInstaller();
var exePath = Path.Combine(configService.ApplicationFolder(), SERVICEEXE);
if (!File.Exists(exePath) && Debugger.IsAttached)
{
exePath = Path.Combine(configService.ApplicationFolder(), "..\\..\\..\\Jackett.Service\\bin\\Debug", SERVICEEXE);
}
string[] cmdline = { @"/assemblypath=" + exePath};
var context = new InstallContext("jackettservice_install.log", cmdline);
serviceInstaller.Context = context;
serviceInstaller.DisplayName = NAME;
serviceInstaller.ServiceName = NAME;
serviceInstaller.Description = DESCRIPTION;
serviceInstaller.StartType = ServiceStartMode.Automatic;
serviceInstaller.Parent = installer;
serviceInstaller.Install(new ListDictionary());
}
}
示例4: 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);
}
}
}
示例5: SafeMain
static void SafeMain(string[] args)
{
AddERExcludedApplication(Process.GetCurrentProcess().MainModule.ModuleName);
Console.WriteLine("TraceSpy Service - " + (Environment.Is64BitProcess ? "64" : "32") + "-bit - Build Number " + Assembly.GetExecutingAssembly().GetInformationalVersion());
Console.WriteLine("Copyright (C) SoftFluent S.A.S 2012-" + DateTime.Now.Year + ". All rights reserved.");
var token = Extensions.GetTokenElevationType();
if (token != TokenElevationType.Full)
{
Console.WriteLine("");
Console.WriteLine("Warning: token elevation type (UAC level) is " + token + ". You may experience access denied errors from now on. You may fix these errors if you restart with Administrator rights or without UAC.");
Console.WriteLine("");
}
OptionHelp = CommandLineUtilities.GetArgument(args, "?", false);
if (!OptionHelp)
{
OptionHelp = CommandLineUtilities.GetArgument(args, "h", false);
if (!OptionHelp)
{
OptionHelp = CommandLineUtilities.GetArgument(args, "help", false);
}
}
OptionService = CommandLineUtilities.GetArgument(args, "s", false);
if (!OptionService)
{
if (OptionHelp)
{
Console.WriteLine("Format is " + Assembly.GetExecutingAssembly().GetName().Name + ".exe [options]");
Console.WriteLine("[options] can be a combination of the following:");
Console.WriteLine(" /? Displays this help");
Console.WriteLine(" /i Installs the <name> service");
Console.WriteLine(" /k Kills this process on any exception");
Console.WriteLine(" /u Uninstalls the <name> service");
Console.WriteLine(" /t Displays traces on the console");
Console.WriteLine(" /l:<name> Locale used");
Console.WriteLine(" default is " + CultureInfo.CurrentCulture.LCID);
Console.WriteLine(" /name:<name> (Un)Installation uses <name> for the service name");
Console.WriteLine(" default is \"" + DefaultName + "\"");
Console.WriteLine(" /displayName:<dname> (Un)Installation uses <dname> for the display name");
Console.WriteLine(" default is \"" + DefaultDisplayName + "\"");
Console.WriteLine(" /description:<desc.> Installation ses <desc.> for the service description");
Console.WriteLine(" default is \"" + DefaultDisplayName + "\"");
Console.WriteLine(" /startType:<type> Installation uses <type> for the service start mode");
Console.WriteLine(" default is \"" + ServiceStartMode.Manual + "\"");
Console.WriteLine(" Values are " + ServiceStartMode.Automatic + ", " + ServiceStartMode.Disabled + " or " + ServiceStartMode.Manual);
Console.WriteLine(" /user:<name> Name of the account under which the service should run");
Console.WriteLine(" default is Local System");
Console.WriteLine(" /password:<text> Password to the account name");
Console.WriteLine(" /config:<path> Path to the configuration file");
Console.WriteLine(" /dependson:<list> A comma separated list of service to depend on");
Console.WriteLine("");
Console.WriteLine("Examples:");
Console.WriteLine(Assembly.GetExecutingAssembly().GetName().Name + " /i /name:MyService /displayName:\"My Service\" /startType:Automatic");
Console.WriteLine(Assembly.GetExecutingAssembly().GetName().Name + " /u /name:MyOtherService");
return;
}
}
OptionTrace = CommandLineUtilities.GetArgument(args, "t", false);
OptionKillOnException = CommandLineUtilities.GetArgument(args, "k", false);
OptionInstall = CommandLineUtilities.GetArgument(args, "i", false);
OptionStartType = (ServiceStartMode)CommandLineUtilities.GetArgument(args, "starttype", ServiceStartMode.Manual);
OptionLcid = CommandLineUtilities.GetArgument<string>(args, "l", null);
OptionUninstall = CommandLineUtilities.GetArgument(args, "u", false);
OptionAccount = (ServiceAccount)CommandLineUtilities.GetArgument(args, "user", ServiceAccount.User);
OptionPassword = CommandLineUtilities.GetArgument<string>(args, "password", null);
OptionUser = CommandLineUtilities.GetArgument<string>(args, "user", null);
string dependsOn = CommandLineUtilities.GetArgument<string>(args, "dependson", null);
if (!string.IsNullOrEmpty(dependsOn))
{
OptionDependsOn = dependsOn.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
}
else
{
OptionDependsOn = null;
}
OptionConfigPath = CommandLineUtilities.GetArgument<string>(args, "config", null);
if (!string.IsNullOrEmpty(OptionConfigPath) && !Path.IsPathRooted(OptionConfigPath))
{
OptionConfigPath = Path.GetFullPath(OptionConfigPath);
}
_configuration = ServiceSection.Get(OptionConfigPath);
OptionDisplayName = CommandLineUtilities.GetArgument(args, "displayname", DefaultDisplayName);
OptionDescription = CommandLineUtilities.GetArgument(args, "description", DefaultDescription);
if (OptionInstall)
{
ServiceInstaller si = new ServiceInstaller();
ServiceProcessInstaller spi = new ServiceProcessInstaller();
si.ServicesDependedOn = OptionDependsOn;
Console.WriteLine("OptionAccount=" + OptionAccount);
Console.WriteLine("OptionUser=" + OptionUser);
if (OptionAccount == ServiceAccount.User)
{
if (string.IsNullOrEmpty(OptionUser))
{
// the default
//.........这里部分代码省略.........