コード例 #1
0
        public void InstallService(Assembly assembly)
        {
            if (IsServiceInstalled())
            {
                return;
            }

            using (AssemblyInstaller installer = GetInstaller(assembly))
            {
                IDictionary state = new Hashtable();
                try
                {
                    installer.Install(state);
                    installer.Commit(state);
                    installer.Dispose();
                }
                catch
                {
                    try
                    {
                        installer.Rollback(state);
                        installer.Dispose();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    throw;
                }
            }
        }
コード例 #2
0
        private void UnInstall()
        {
            if (!ServiceExist())
            {
                MessageBox.Show("系统不存在此服务,不需要卸载!");
                return;
            }
            label5.Text = "正在卸载";
            try {
                ServiceStop();
                string            location           = System.Reflection.Assembly.GetExecutingAssembly().Location;
                string            serviceFileName    = location.Substring(0, location.LastIndexOf('\\') + 1) + "xqnsvc.exe";
                AssemblyInstaller AssemblyInstaller1 = new AssemblyInstaller();
                AssemblyInstaller1.UseNewContext = true;
                AssemblyInstaller1.Path          = serviceFileName;
                AssemblyInstaller1.Uninstall(null);
                AssemblyInstaller1.Dispose();
                label5.Text = "卸载完成";

                button3.Enabled = true;
                安装服务IToolStripMenuItem.Enabled = true;
                button4.Enabled = false;
                卸载服务RToolStripMenuItem.Enabled = false;
            }
            catch // (Exception ex)
            {
                MessageBox.Show("发生错误:请尝试右键,以管理员身份运行后再操作!");
                label5.Text = "卸载异常";
            }
        }
コード例 #3
0
        // 安装服务
        public static void InstallService(string serviceName, string strServiceInstallPath)
        {
            IDictionary mySavedState = new Hashtable();

            try
            {
                if (!ServiceIsExisted(serviceName))
                {
                    // 安装服务
                    AssemblyInstaller assemblyInstaller = new AssemblyInstaller();

                    mySavedState.Clear();
                    assemblyInstaller.Path          = strServiceInstallPath;
                    assemblyInstaller.UseNewContext = true;
                    assemblyInstaller.Install(mySavedState);
                    assemblyInstaller.Commit(mySavedState);
                    assemblyInstaller.Dispose();

                    // 将服务设置为自动启动
                    ChangeServiceStartType(2, serviceName);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("安装服务出错:" + ex.Message);
            }
        }
コード例 #4
0
        public static void InstallService2(bool install, string serviceFileName)
        {
            AssemblyInstaller installer;

            if (install)
            {
                installer = new AssemblyInstaller();
                IDictionary stateSaver = new Hashtable();
                installer.UseNewContext = true;
                installer.Path          = serviceFileName;
                stateSaver.Clear();
                installer.Install(stateSaver);
                installer.Commit(stateSaver);
                installer.Dispose();
            }
            else
            {
                installer = new AssemblyInstaller {
                    UseNewContext = true,
                    Path          = serviceFileName
                };
                installer.Uninstall(null);
                installer.Dispose();
            }
        }
コード例 #5
0
ファイル: ServiceTool.cs プロジェクト: Jaasdsa/CityIoT
 /// <summary>
 /// 安装服务
 /// </summary>
 /// <param name="stateSaver"></param>
 /// <param name="filepath"></param>
 private void InstallService(IDictionary stateSaver, string filepath, string serviceName)
 {
     try
     {
         System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(serviceName);
         if (!IsExistedService(serviceName))
         {
             //Install Service
             AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller();
             myAssemblyInstaller.UseNewContext = true;
             myAssemblyInstaller.Path          = filepath;
             myAssemblyInstaller.Install(stateSaver);
             myAssemblyInstaller.Commit(stateSaver);
             myAssemblyInstaller.Dispose();
             //--Start Service
             service.Start();
         }
         else
         {
             if (service.Status != System.ServiceProcess.ServiceControllerStatus.Running && service.Status != System.ServiceProcess.ServiceControllerStatus.StartPending)
             {
                 service.Start();
             }
         }
     }
     catch (Exception ex)
     {
         throw new Exception("installServiceError/n" + ex.Message);
     }
 }
コード例 #6
0
    {/// <summary>
     /// 安装.Net服务,返回是否安装成功
     /// </summary>
     /// <param name="svcPath">服务程序目标路径</param>
     /// <param name="logToConsole">是否将安装日志回显至控制台</param>
     /// <returns>是否安装成功</returns>
        public static bool InstallService(string svcPath, bool logToConsole)
        {
            var title        = "Install " + Path.GetFileNameWithoutExtension(svcPath);
            var logtoConsole = "/LogtoConsole=" + logToConsole.ToString().ToLower();
            var assInstall   = new AssemblyInstaller(svcPath, new string[] { logtoConsole });
            var installState = new Hashtable();

            try
            {
                assInstall.Install(installState);
                assInstall.Commit(installState);
                //Trace.Write(Trace.LogType.Installation, title, "Successful.");
                return(true);
            }
            catch (Exception exp)
            {
                //Trace.Write(Trace.LogType.Installation, title, exp);
                try
                {
                    assInstall.Rollback(installState);
                }
                catch
                {
                }
                return(false);
            }
            finally
            {
                assInstall.Dispose();
            }
        }
コード例 #7
0
        public void InstallService(IDictionary stateSaver, string filepath)
        {
            a : try
            {
                ServiceController service = new ServiceController("金建软件服务");
                if (!ServiceIsExisted("金建软件服务"))
                {
                    //Install Service
                    AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller();
                    myAssemblyInstaller.UseNewContext = true;
                    myAssemblyInstaller.Path          = filepath;
                    myAssemblyInstaller.Install(stateSaver);
                    myAssemblyInstaller.Commit(stateSaver);
                    myAssemblyInstaller.Dispose();
                    //--Start Service
                    service.Start();
                }
                else
                {
                    UnInstallService(filepath);
                    goto a;

                    //if (service.Status != ServiceControllerStatus.Running && service.Status != System.ServiceProcess.ServiceControllerStatus.StartPending)
                    //{
                    //    service.Start();
                    //}
                }
            }
            catch (Exception ex)
            {
                throw new Exception("installServiceError\n" + ex.Message);
            }
        }
コード例 #8
0
        /// <summary>
        /// 卸载服务
        /// </summary>
        /// <param name="exePath">服务程序路径</param>
        public static void UnistallByPath(string exePath)
        {
            if (string.IsNullOrEmpty(exePath))
            {
                return;
            }

            AssemblyInstaller installer = new AssemblyInstaller();

            try
            {
                IDictionary state = new Hashtable();
                installer.Path          = exePath;
                installer.UseNewContext = true;
                installer.Uninstall(state);
                installer.Commit(state);
            }
            catch (Exception e)
            {
            }
            finally
            {
                installer.Dispose();
            }
        }
コード例 #9
0
        public static void InstallService(string serviceName, string serviceInstallPath)
        {
            //var strServiceName = "KugarDataSyncClient";
            //var strServiceInstallPath = System.Environment.CommandLine;

            IDictionary mySavedState = new Hashtable();

            try
            {
                System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(serviceName);

                //服务已经存在则卸载
                if (ServiceIsExisted(serviceName))
                {
                    //StopService(strServiceName);
                    UnInstallService(serviceName, serviceInstallPath);
                }
                service.Refresh();
                //注册服务
                AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller();

                mySavedState.Clear();
                myAssemblyInstaller.Path          = serviceInstallPath;
                myAssemblyInstaller.UseNewContext = true;
                myAssemblyInstaller.Install(mySavedState);
                myAssemblyInstaller.Commit(mySavedState);
                myAssemblyInstaller.Dispose();

                service.Start();
            }
            catch (Exception ex)
            {
                throw new Exception("注册服务时出错:" + ex.Message);
            }
        }
コード例 #10
0
ファイル: ServiceAPI.cs プロジェクト: angleC/AutoUpdate
        /// <summary>
        /// 卸载Windows服务
        /// </summary>
        /// <param name="serviceFilePath">程序文件路径</param>
        internal static void UnInstallService(string serviceFilePath)
        {
            AssemblyInstaller assemblyInstaller = new AssemblyInstaller();

            assemblyInstaller.UseNewContext = true;
            assemblyInstaller.Path          = serviceFilePath;
            assemblyInstaller.Uninstall(null);
            assemblyInstaller.Dispose();
        }
コード例 #11
0
ファイル: frmMain.cs プロジェクト: rwj42/MobileERP
        //根据文件路径卸载服务
        private static void UnInstallmyService(string filepath)
        {
            var iAS = new AssemblyInstaller();

            iAS.UseNewContext = true;
            iAS.Path          = filepath;
            iAS.Uninstall(null);
            iAS.Dispose();
        }
コード例 #12
0
        /// <summary>
        /// 卸载Windows服务
        /// </summary>
        /// <param name="filepath">程序文件路径</param>
        public static void UnInstallService(string filepath)
        {
            AssemblyInstaller installer = new AssemblyInstaller();

            installer.UseNewContext = true;
            installer.Path          = filepath;
            installer.Uninstall(null);
            installer.Dispose();
        }
コード例 #13
0
        /// <summary>
        /// 卸载Windows服务
        /// </summary>
        /// <param name="filepath">程序文件路径</param>
        public static void UnInstallmyService(string filepath)
        {
            AssemblyInstaller AssemblyInstaller1 = new AssemblyInstaller();

            AssemblyInstaller1.UseNewContext = true;
            AssemblyInstaller1.Path          = filepath;
            AssemblyInstaller1.Uninstall(null);
            AssemblyInstaller1.Dispose();
        }
コード例 #14
0
        /// <summary>
        /// 卸载Windows服务
        /// </summary>
        /// <param name="filepath">程序文件路径</param>
        public static void UnInstallByFilePath(string filepath)
        {
            var asmInstaller = new AssemblyInstaller();

            asmInstaller.UseNewContext = true;
            asmInstaller.Path          = filepath;
            asmInstaller.Uninstall(null);
            asmInstaller.Dispose();
        }
コード例 #15
0
        /// <summary>
        /// 卸载Windows服务
        /// </summary>
        /// <param name="filepath">程序文件路径</param>
        public static void UnInstallService(string filepath)
        {
            AssemblyInstaller assemblyInstaller1 = new AssemblyInstaller {
                UseNewContext = true, Path = filepath
            };

            assemblyInstaller1.Uninstall(null);
            assemblyInstaller1.Dispose();
        }
コード例 #16
0
ファイル: Installer1.cs プロジェクト: zzz1985xxxwl/bjl_hrmis
        private static void UnInstallService(string serverDir)
        {
            //UnInstall Service
            AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller();

            myAssemblyInstaller.UseNewContext = true;
            myAssemblyInstaller.Path          = serverDir;
            myAssemblyInstaller.Uninstall(null);
            myAssemblyInstaller.Dispose();
        }
コード例 #17
0
        /// <summary>
        /// 安装Windows服务
        /// </summary>
        /// <param name="stateSaver">集合</param>
        /// <param name="filepath">程序文件路径</param>
        public static void InstallService(IDictionary stateSaver, string filepath)
        {
            AssemblyInstaller assemblyInstaller1 = new AssemblyInstaller {
                UseNewContext = true, Path = filepath
            };

            assemblyInstaller1.Install(stateSaver);
            assemblyInstaller1.Commit(stateSaver);
            assemblyInstaller1.Dispose();
        }
コード例 #18
0
        /// <summary>
        /// 安装Windows服务
        /// </summary>
        /// <param name="stateSaver">集合</param>
        /// <param name="filepath">程序文件路径</param>
        public static void InstallService(IDictionary stateSaver, string filepath)
        {
            AssemblyInstaller installer = new AssemblyInstaller();

            installer.UseNewContext = true;
            installer.Path          = filepath;
            installer.Install(stateSaver);
            installer.Commit(stateSaver);
            installer.Dispose();
        }
コード例 #19
0
        /// <summary>
        /// 安装Windows服务
        /// </summary>
        /// <param name="stateSaver">集合</param>
        /// <param name="filepath">程序文件路径</param>
        public static void InstallmyService(IDictionary stateSaver, string filepath)
        {
            AssemblyInstaller AssemblyInstaller1 = new AssemblyInstaller();

            AssemblyInstaller1.UseNewContext = true;
            AssemblyInstaller1.Path          = filepath;
            AssemblyInstaller1.Install(stateSaver);
            AssemblyInstaller1.Commit(stateSaver);
            AssemblyInstaller1.Dispose();
        }
コード例 #20
0
ファイル: ServiceAPI.cs プロジェクト: angleC/AutoUpdate
        /// <summary>
        /// 安装Windows服务
        /// </summary>
        /// <param name="stateSaver">集合</param>
        /// <param name="serviceFilePath">程序文件路径</param>
        internal static void InstallService(IDictionary stateSaver, string serviceFilePath)
        {
            AssemblyInstaller assemblyInstaller = new AssemblyInstaller();

            assemblyInstaller.UseNewContext = true;
            assemblyInstaller.Path          = serviceFilePath;
            assemblyInstaller.Install(stateSaver);
            assemblyInstaller.Commit(stateSaver);
            assemblyInstaller.Dispose();
        }
コード例 #21
0
 /// <summary>
 /// 卸载服务
 /// </summary>
 /// <param name="filepath"></param>
 /// <param name="serviceName"></param>
 public static void UnInstallService(String filepath, String serviceName)
 {
     if (ServiceExist(serviceName))
     {
         AssemblyInstaller ass = new AssemblyInstaller();
         ass.UseNewContext = true;
         ass.Path          = filepath;
         ass.Uninstall(null);
         ass.Dispose();
     }
 }
コード例 #22
0
 /// <summary>
 /// 卸载windows服务
 /// </summary>
 /// <param name="filepath">获取或设置要安装的程序集的路径。</param>
 public static void UnInstallService(String serviceName, string filepath)
 {
     if (ServiceIsExisted(serviceName))
     {
         AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller();
         myAssemblyInstaller.UseNewContext = true;
         myAssemblyInstaller.Path          = filepath;
         myAssemblyInstaller.Uninstall(null);
         myAssemblyInstaller.Dispose();
     }
 }
コード例 #23
0
        public void UnInstallService(string exeFileName)
        {
            if (!File.Exists(exeFileName))
            {
                return;
            }

            AssemblyInstaller installer = new AssemblyInstaller(exeFileName, null);

            installer.UseNewContext = true;
            installer.Uninstall(null);
            installer.Dispose();
        }
コード例 #24
0
        public void InstallService(string exeFileName, string[] args)
        {
            if (!File.Exists(exeFileName))
            {
                return;
            }
            AssemblyInstaller installer = new AssemblyInstaller(exeFileName, args);

            installer.UseNewContext = true;
            installer.Install(null);
            installer.Commit(null);
            installer.Dispose();
        }
コード例 #25
0
 /// <summary>
 /// 卸载服务
 /// </summary>
 /// <param name="filepath"></param>
 public static void uninstallmyservice(string filepath)
 {
     try
     {
         AssemblyInstaller assemblyinstaller1 = new AssemblyInstaller();
         assemblyinstaller1.UseNewContext = true;
         assemblyinstaller1.Path          = filepath;
         assemblyinstaller1.Uninstall(null);
         assemblyinstaller1.Dispose();
     }
     catch (Exception e)
     {
         throw new Exception("请以管理员身份启动程序再执行此操作\n" + e.Message);
     }
 }
コード例 #26
0
        /// <summary>
        /// 安装windowservice
        /// </summary>
        /// <param name="serviceName">WindowService名称</param>
        /// <param name="serviceProgramFileFullPath">服务的执行程序完整路径</param>
        /// <returns>成功返回 true,否则返回 false;</returns>
        public static bool InstallService(string serviceName, string serviceProgramFileFullPath)
        {
            if (IsServiceIsExisted(serviceName))
            {
                return(true);
            }
            TransactedInstaller transactedInstaller = new TransactedInstaller();
            AssemblyInstaller   assemblyInstaller   = new AssemblyInstaller(serviceProgramFileFullPath, new string[] {});

            transactedInstaller.Installers.Add(assemblyInstaller);
            transactedInstaller.Install(new Hashtable());
            assemblyInstaller.Dispose();
            transactedInstaller.Dispose();
            return(IsServiceIsExisted(serviceName));
        }
コード例 #27
0
        public static void InstallService()
        {
            string exePath  = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
            string strDir   = System.IO.Path.GetDirectoryName(exePath);
            string filepath = strDir + "\\MPlayerWWService.exe";

            IDictionary       mSavedState         = new Hashtable();
            AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller();

            myAssemblyInstaller.UseNewContext = true;
            myAssemblyInstaller.Path          = filepath;
            myAssemblyInstaller.Install(mSavedState);
            myAssemblyInstaller.Commit(mSavedState);
            myAssemblyInstaller.Dispose();
        }
コード例 #28
0
        /// <summary>
        /// 卸载windowservice
        /// </summary>
        /// <param name="serviceName">WindowService名称</param>
        /// <param name="serviceProgramFullPath">服务的执行程序完整路径</param>
        /// <returns>成功返回 true,否则返回 false;</returns>
        public static bool UninstallService(string serviceName, string serviceProgramFullPath)
        {
            if (!IsServiceIsExisted(serviceName))
            {
                return(true);
            }
            string[]            cmdline             = {};
            TransactedInstaller transactedInstaller = new TransactedInstaller();
            AssemblyInstaller   assemblyInstaller   = new AssemblyInstaller(serviceProgramFullPath, cmdline);

            transactedInstaller.Installers.Add(assemblyInstaller);
            transactedInstaller.Uninstall(null);
            assemblyInstaller.Dispose();
            transactedInstaller.Dispose();
            return(!IsServiceIsExisted(serviceName));
        }
コード例 #29
0
 /// <summary>
 /// 卸载服务
 /// </summary>
 /// <param name="serviceFileName"></param>
 /// <returns></returns>
 public static bool UnInstallService(string serviceFileName)
 {
     try
     {
         var installer = new AssemblyInstaller();
         installer.UseNewContext = true;
         installer.Path          = serviceFileName;
         installer.Uninstall(null);
         installer.Dispose();
         return(true);
     }
     catch
     {
         return(false);
     }
 }
コード例 #30
0
 public void UninstallService(Assembly assembly)
 {
     btnInstallUninstall.Text = "Processing...";
     using (AssemblyInstaller installer = GetInstaller(assembly))
     {
         try
         {
             installer.Uninstall(null);
             installer.Dispose();
         }
         catch (Exception ex)
         {
             MessageBox.Show(ex.Message);
         }
     }
 }