当前位置: 首页>>代码示例>>C#>>正文


C# ServiceInstaller.Commit方法代码示例

本文整理汇总了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);
                    }
                }
            }
        }
开发者ID:BobbyCannon,项目名称:HostR,代码行数:59,代码来源:WindowsServiceInstaller.cs

示例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);
                }
            }
        }
开发者ID:1aurent,项目名称:CloudBackup,代码行数:64,代码来源:ProgramSupport.cs


注:本文中的System.ServiceProcess.ServiceInstaller.Commit方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。