コード例 #1
0
ファイル: Program.cs プロジェクト: robertbreker/cloudstack
 private static AssemblyInstaller GetInstaller()
 {
     AssemblyInstaller installer = new AssemblyInstaller(
         typeof(Program).Assembly, null);
     installer.UseNewContext = true;
     return installer;
 }
コード例 #2
0
ファイル: ServiceHelper.cs プロジェクト: GHLabs/SambaPOS-3
        public static bool UninstallWindowsService()
        {
            StopService();
            IDictionary mySavedState = new Hashtable();

            try
            {
                // Set the commandline argument array for 'logfile'.
                string[] commandLineOptions = new string[1] { string.Format("/LogFile={0}", _logFile) };

                // Create an object of the 'AssemblyInstaller' class.
                AssemblyInstaller myAssemblyInstaller = new
                AssemblyInstaller(_installAssembly, commandLineOptions);


                myAssemblyInstaller.UseNewContext = true;

                // Install the 'MyAssembly' assembly.
                myAssemblyInstaller.Uninstall(mySavedState);

                // Commit the 'MyAssembly' assembly.
                myAssemblyInstaller.Commit(mySavedState);
            }
            catch (Exception e)
            { return false; }

            return true;
        }
コード例 #3
0
ファイル: ServicesHelper.cs プロジェクト: yonglehou/BPMSV1
 /// <summary>
 /// 安装windows服务
 /// </summary>
 private void InstallService(IDictionary stateSaver, string filepath, string serviceName)
 {
     try
     {
         ServiceController service = new ServiceController(serviceName);
         if (!ServiceIsExisted(serviceName))
         {
             AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller();
             myAssemblyInstaller.UseNewContext = true;
             myAssemblyInstaller.Path = filepath;
             myAssemblyInstaller.Install(stateSaver);
             myAssemblyInstaller.Commit(stateSaver);
             myAssemblyInstaller.Dispose();
             service.Start();
         }
         else
         {
             if (service.Status != ServiceControllerStatus.Running && service.Status != ServiceControllerStatus.StartPending)
                 service.Start();
         }
     }
     catch (Exception ex)
     {
         throw new Exception("InstallServiceError\r\n" + ex.Message);
     }
 }
コード例 #4
0
ファイル: InstallUtil.cs プロジェクト: vjohnson01/Tools
        private void Install(AssemblyInstaller installer, IDictionary state, bool undo)
        {

            try
            {
                if (undo)
                {
                    _log.Write(LogLevel.Info, "Uninstalling {0}...", StringConstants.ServiceName);
                    installer.Uninstall(state);
                    _log.Write(LogLevel.Info, "{0} has been successfully removed from the system.", StringConstants.ServiceName);
                }
                else
                {
                    _log.Write(LogLevel.Info, "Installing {0}...", StringConstants.ServiceName);
                    installer.Install(state);

                    _log.Write(LogLevel.Info, "Commiting changes...");
                    installer.Commit(state);

                    _log.Write(LogLevel.Info, "Install succeeded.");
                }
            }
            catch (Exception ex)
            {
                _log.Write(LogLevel.Error, "An error occured during {1}. {0}", ex, undo?"uninstall" : "install");
                _log.Write(LogLevel.Info, "Trying to roll back...");
                TryRollback(installer, state);
            }
        }
コード例 #5
0
 /// <summary>
 /// 安装服务:
 /// </summary>
 /// <param name="filepath"></param>
 public void InstallService(string filepath)
 {
     try
     {
         string serviceName = GetServiceName(filepath);
         ServiceController service = new ServiceController(serviceName);
         if (!ServiceIsExisted(serviceName))
         {
             //Install Service
             AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller() { UseNewContext = true, Path = filepath };
             myAssemblyInstaller.Install(new Hashtable());
             myAssemblyInstaller.Commit(new Hashtable());
             myAssemblyInstaller.Dispose();
             //--Start Service
             service.Start();
         }
         else
         {
             if (service.Status != ServiceControllerStatus.Running && service.Status != ServiceControllerStatus.StartPending)
             {
                 service.Start();
             }
         }
     }
     catch (Exception ex)
     {
         throw new Exception("installServiceError/n" + ex.Message);
     }
 }
コード例 #6
0
ファイル: FrmMain.cs プロジェクト: Zorbam/TaskManager
 /// <summary>
 /// 安装服务
 /// </summary>
 private void btnInstall_Click(object sender, EventArgs e)
 {
     if (!Vaild())
     {
         return;
     }
     try
     {
         string[] cmdline = { };
         string serviceFileName = txtPath.Text.Trim();
         string serviceName = GetServiceName(serviceFileName);
         if (string.IsNullOrEmpty(serviceName))
         {
             txtTip.Text = "指定文件不是Windows服务!";
             return;
         }
         if (ServiceIsExisted(serviceName))
         {
             txtTip.Text = "要安装的服务已经存在!";
             return;
         }
         TransactedInstaller transactedInstaller = new TransactedInstaller();
         AssemblyInstaller assemblyInstaller = new AssemblyInstaller(serviceFileName, cmdline);
         assemblyInstaller.UseNewContext = true;
         transactedInstaller.Installers.Add(assemblyInstaller);
         transactedInstaller.Install(new System.Collections.Hashtable());
         txtTip.Text = "服务安装成功!";
     }
     catch (Exception ex)
     {
         txtTip.Text = ex.Message;
     }
 }
コード例 #7
0
ファイル: ServiceControl.cs プロジェクト: erynet/IMS
 private static AssemblyInstaller GetInstaller(System.Reflection.Assembly assem)
 {
     AssemblyInstaller installer = new AssemblyInstaller(
         assem, null);
     installer.UseNewContext = true;
     return installer;
 }
コード例 #8
0
        public static void Uninstall(string[] args)
        {
            try
            {
                using (var installer = new AssemblyInstaller(typeof(InstallationManager).Assembly, args))
                {
                    IDictionary state = new Hashtable();

                    // Install the service
                    installer.UseNewContext = true;
                    try
                    {
                        installer.Uninstall(state);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.ToString());

                        try
                        {
                            installer.Rollback(state);
                        }
                        catch (Exception exception)
                        {
                            Console.WriteLine(exception.ToString());
                        }
                    }
                }
            }
            catch (Exception exception)
            {

                Console.WriteLine("Failed to install service. Error: " + exception.Message);
            }
        }
コード例 #9
0
ファイル: ServiceUtil.cs プロジェクト: jihadbird/firespider
 /// <summary>
 /// 安装服务。
 /// </summary>
 /// <param name="fileName">文件名称</param>
 /// <param name="args">命令行参数</param>
 public static void InstallService(string fileName, string[] args)
 {
     TransactedInstaller transactedInstaller = new TransactedInstaller();
     AssemblyInstaller assemblyInstaller = new AssemblyInstaller(fileName, args);
     transactedInstaller.Installers.Add(assemblyInstaller);
     transactedInstaller.Install(new System.Collections.Hashtable());
 }
コード例 #10
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="assembly"></param>
 /// <returns></returns>
 public static AssemblyInstaller GetInstaller(string serviceName)
 {
     //TODO: THis is not working
     AssemblyInstaller installer = new AssemblyInstaller();
     installer.UseNewContext = true;
     return installer;
 }
コード例 #11
0
ファイル: ServiceHelper.cs プロジェクト: GHLabs/SambaPOS-3
        private static bool ServiceInstaller(bool uninstall = false)
        {
            IDictionary mySavedState = new Hashtable();

            try
            {
                // Set the commandline argument array for 'logfile'.
                string[] commandLineOptions = new string[1] { string.Format("/LogFile={0}", _logFile) };

                // Create an object of the 'AssemblyInstaller' class.
                AssemblyInstaller myAssemblyInstaller = new
                AssemblyInstaller(_installAssembly, commandLineOptions);

                myAssemblyInstaller.UseNewContext = true;

                if (!uninstall)
                {
                    myAssemblyInstaller.Install(mySavedState);
                    // Commit the 'MyAssembly' assembly.
                    myAssemblyInstaller.Commit(mySavedState);
                }
                else
                { myAssemblyInstaller.Uninstall(mySavedState); }
            }
            catch (FileNotFoundException)
            {
                throw;
            }
            catch (Exception)
            { return false; }
            
            return true;
        }
コード例 #12
0
 public void InstallTheService()
 {
     try
     {
         IDictionary state = new Hashtable();
         using (AssemblyInstaller installer = new AssemblyInstaller(InstallersAssembly, Args))
         {
             installer.UseNewContext = true;
             try
             {
                 installer.Install(state);
                 installer.Commit(state);
                 InternalTrace("Installed the service");
             }
             catch (Exception installException)
             {
                 try
                 {
                     installer.Rollback(state);
                     InternalTrace("Rolledback the service installation because:" + installException.ToString());
                 }
                 catch { }
                 throw;
             }
         }
     }
     catch (Exception exception)
     {
         InternalTrace("Failed to install the service " + exception.ToString());
     }
 }
コード例 #13
0
 static void Install(bool undo, string[] args)
 {
     try
     {
         Console.WriteLine(undo ? "Uninstalling" : "Installing");
         using (AssemblyInstaller inst = new AssemblyInstaller(typeof(WinSvc).Assembly, args))
         {
             IDictionary state = new Hashtable();
             inst.UseNewContext = true;
             try
             {
                 if (undo) inst.Uninstall(state);
                 else
                 {
                     inst.Install(state);
                     inst.Commit(state);
                 }
             }
             catch
             {
                 try { inst.Rollback(state); }
                 catch { }
                 throw;
             }
         }
         Console.WriteLine("Done");
     }
     catch (Exception ex)
     {
         Console.Error.WriteLine(ex.Message);
     }
 }
コード例 #14
0
        public Installation() : base() 
        {
            #region Install on MSR.LST.Net.Rtp (a necessary dependency)
            AssemblyInstaller ai = new AssemblyInstaller();
            ai.UseNewContext = true;
            ai.Assembly = Assembly.Load("MSR.LST.Net.Rtp");
            Installers.Add(ai);
            #endregion
            
            #region Install MDShowManager (if it is in the same directory)
            FileInfo fi = new FileInfo(Assembly.GetExecutingAssembly().Location);
            FileInfo[] foundFiles = fi.Directory.GetFiles("MDShowManager.dll");
            if (foundFiles.Length == 1)
            {
                ai = new AssemblyInstaller();
                ai.UseNewContext = true;
                ai.Path = foundFiles[0].FullName;
                Installers.Add(ai);
            }
            #endregion

            #region Install Pipecleaner Agent Service (if it is in the same directory)
            fi = new FileInfo(Assembly.GetExecutingAssembly().Location);
            foundFiles = fi.Directory.GetFiles("Pipecleaner Agent Service.exe");
            if (foundFiles.Length == 1)
            {
                ai = new AssemblyInstaller();
                ai.UseNewContext = true;
                ai.Path = foundFiles[0].FullName;
                Installers.Add(ai);
            }
            #endregion
        }
コード例 #15
0
ファイル: Installer.cs プロジェクト: abhishek-kumar/AIGA
 public Installation() : base() 
 {
     // - Add the installer for MSR.LST.Net.Rtp to the list
     AssemblyInstaller rtpInstall = new AssemblyInstaller();
     rtpInstall.Assembly = Assembly.Load("MSR.LST.Net.Rtp");
     Installers.Add(rtpInstall);
 }
コード例 #16
0
        public void InstallService(string user, string pass) {
            using (AssemblyInstaller installer = new AssemblyInstaller(Path.Combine(Utils.GetApplicationPath(), Utils.GetExecutableName()), null)) {
                installer.UseNewContext = true;
                Hashtable savedState = new Hashtable();

                UserPassCombination c = new UserPassCombination();
                c.User = user;
                c.Pass = pass;
                _userPassCombination = c;

                installer.BeforeInstall += new InstallEventHandler(installer_BeforeInstall);

                //Rollback has to be called by user code. According msdn-doc for AssemblyInstaller.Install() is probably wrong.
                try {
                    installer.Install(savedState);
                    installer.Commit(savedState);
                }
                catch (Exception ex) {
                    installer.Rollback(savedState);
                    throw new InstallException(String.Format("Install failed: {0}", ex.Message), ex);
                }

                installer.BeforeInstall -= installer_BeforeInstall;
            }
        }
コード例 #17
0
ファイル: Program.cs プロジェクト: W8023Y2014/jsion
        private static void Main(string[] args)
        {
            AppDomain.CurrentDomain.SetupInformation.PrivateBinPath = "." + Path.DirectorySeparatorChar + "lib";

            Thread.CurrentThread.Name = "MAIN";

            AssemblyInstaller installer = new AssemblyInstaller(Assembly.GetExecutingAssembly(), null);

            Hashtable rollback = new Hashtable();

            try
            {
                //installer.Install(rollback);
                //installer.Commit(rollback);
                ////installer.Uninstall(rollback);
            }
            catch (Exception ex)
            {
                //installer.Rollback(rollback);
                Console.WriteLine("Error installing as system service");
                Console.WriteLine(ex.Message);
                //Console.ReadKey();
            }
            Console.ReadKey();
        }
コード例 #18
0
ファイル: Program.cs プロジェクト: Wryxo/bakalarka
 private static AssemblyInstaller GetInstaller()
 {
     AssemblyInstaller installer = new AssemblyInstaller(
         typeof(SetItUpService).Assembly, null);
     installer.UseNewContext = true;
     return installer;
 }
コード例 #19
0
 /// <summary>
 /// 安装Windows服务
 /// </summary>
 /// <param name="stateSaver">状态集合</param>
 /// <param name="filepath">程序文件路径</param>
 public static void InstallService(IDictionary stateSaver, String filepath)
 {
     AssemblyInstaller AssemblyInstaller1 = new AssemblyInstaller();
     AssemblyInstaller1.UseNewContext = true;
     AssemblyInstaller1.Path = filepath;
     AssemblyInstaller1.Install(stateSaver);
     AssemblyInstaller1.Commit(stateSaver);
     AssemblyInstaller1.Dispose();
 }
コード例 #20
0
ファイル: InstallUtil.cs プロジェクト: vjohnson01/Tools
        private void TryRollback(AssemblyInstaller installer, IDictionary state)
        {
            try { installer.Rollback(state); }
            catch (Exception ex )
            {
                _log.Write(LogLevel.Warning, "An error occured during rollback. {0}", ex);
            }

        }
コード例 #21
0
        // Print help for a specific installer assembly.
        private void PrintHelpFor(String filename)
        {
            AssemblyInstaller inst;

            inst = new AssemblyInstaller(filename, options);
            Console.WriteLine("Help options for {0}:", filename);
            Console.WriteLine();
            Console.Write(inst.HelpText);
        }
コード例 #22
0
		private TransactedInstaller InitializeInstaller(Dictionary<string, string> parameters)
		{
			SetupParameters(parameters);
			var ti = new TransactedInstaller();
			var ai = new AssemblyInstaller(Path.Combine(_sourcePath, _serviceExecutable), _parameters);
			ti.Installers.Add(ai);
			var ic = new InstallContext("Install.log", _parameters);
			ti.Context = ic;
			return ti;
		}
コード例 #23
0
	public void SetUp ()
	{
		testAssembly = Assembly.LoadFrom (testAssemblyPath);
		args = new string [] { "/Option1", "--Option2", "-Option3=val", "LogFile=", "LogToConsole" };
		ins = new AssemblyInstaller (testAssemblyPath, args);
		ins.UseNewContext = false;
		log = new StringWriter ();
		log.NewLine = "\n";
		Console.SetOut (log);
	}
コード例 #24
0
        /// <summary>
        /// Remove um serviço do windows do registro
        /// </summary>
        public static void UninstallService(String fileName)
        {
            Directory.SetCurrentDirectory(Path.GetDirectoryName(fileName));
            String serviceName = Path.GetFileNameWithoutExtension(fileName);
            String[] arguments = new string[] { "/LogFile=" + serviceName + "_Install.log" };

            AssemblyInstaller installer = new AssemblyInstaller(fileName, arguments);
            installer.UseNewContext = true;
            installer.Uninstall(null);
        }
コード例 #25
0
ファイル: Program.cs プロジェクト: eatage/sqlbackup
        static void Main(string[] args)
        {
            // 运行服务
            if (args.Length == 0)
            {
                ServiceBase[] ServicesToRun;
                ServicesToRun = new ServiceBase[] { new Servicesqlbackup() };
                ServiceBase.Run(ServicesToRun);

            }
            // 安装服务
            else if (args[0].ToLower() == "/i" || args[0].ToLower() == "-i")
            {
                try
                {
                    string[] cmdline = { };
                    string serviceFileName = System.Reflection.Assembly.GetExecutingAssembly().Location;

                    TransactedInstaller transactedInstaller = new TransactedInstaller();
                    AssemblyInstaller assemblyInstaller = new AssemblyInstaller(serviceFileName, cmdline);
                    transactedInstaller.Installers.Add(assemblyInstaller);
                    transactedInstaller.Install(new System.Collections.Hashtable());

                }
                catch (Exception ex)
                {
                    string msg = ex.Message;
                }
            }
            // 删除服务
            else if (args[0].ToLower() == "/u" || args[0].ToLower() == "-u")
            {
                try
                {
                    string[] cmdline = { };
                    string serviceFileName = System.Reflection.Assembly.GetExecutingAssembly().Location;

                    TransactedInstaller transactedInstaller = new TransactedInstaller();
                    AssemblyInstaller assemblyInstaller = new AssemblyInstaller(serviceFileName, cmdline);
                    transactedInstaller.Installers.Add(assemblyInstaller);
                    transactedInstaller.Uninstall(null);
                }
                catch (Exception ex)
                {
                    string msg = ex.Message;
                }
            }
            //以窗口方式运行服务
            else if (args[0].ToLower() == "/f" || args[0].ToLower() == "-f" || args[0].ToLower() == "/form" || args[0].ToLower() == "-form")
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
        }
コード例 #26
0
ファイル: Installer.cs プロジェクト: abhishek-kumar/AIGA
        public Installer()
            : base()
        {
            AssemblyInstaller ai = new AssemblyInstaller();
            ai.Assembly = Assembly.Load("Conference");
            Installers.Add(ai);

            AssemblyInstaller ai2 = new AssemblyInstaller();
            ai2.Assembly = Assembly.Load("LSTCommon");
            Installers.Add(ai2);
        }
コード例 #27
0
ファイル: ServiceUtil.cs プロジェクト: limingnihao/Net
 /// <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();
     }
 }
コード例 #28
0
        /// <summary>
        /// Acrescenta um serviço do windows no registro
        /// </summary>
        public static void InstallService(String fileName)
        {
            Directory.SetCurrentDirectory(Path.GetDirectoryName(fileName));
            String serviceName = Path.GetFileNameWithoutExtension(fileName);
            String[] arguments = new string[] { "/LogFile=" + serviceName + "_Install.log" };
            IDictionary state = new Hashtable();

            AssemblyInstaller installer = new AssemblyInstaller(fileName, arguments);
            installer.UseNewContext = true;
            installer.Install(state);
            installer.Commit(state);
        }
コード例 #29
0
        /// <summary>
        /// Install the service that is contained in specified assembly.
        /// </summary>
        /// <param name="pathToAssembly">Path to service assembly.</param>
        /// <param name="commandLineArguments">Parameters, that are passed to assembly.</param>
        public static void InstallAssembly(string pathToAssembly, string[] commandLineArguments)
        {
            List<string> argList = new List<string>(commandLineArguments);

            argList.Add(string.Format("/LogFile={0}_install.log",
                                        Path.GetFileNameWithoutExtension(pathToAssembly)));
            using (AssemblyInstaller installer = new AssemblyInstaller(pathToAssembly, argList.ToArray())) {
                var state = new Hashtable();
                installer.Install(state);
                installer.Commit(state);
            }
        }
コード例 #30
0
	public void SetUp ()
	{
		Installer[] ins;

		ins = new AssemblyInstaller[3];
		ins[0] = new AssemblyInstaller ();
		ins[1] = new AssemblyInstaller ();
		ins[2] = new AssemblyInstaller ();

		ti = new TransactedInstaller ();
		ic = ti.Installers;
		ic.AddRange (ins);
	}
コード例 #31
0
ファイル: Program.cs プロジェクト: akshaynm87/FamServer
        static void Install(bool undo, string[] args)
        {
            try
            {
                Console.WriteLine(undo ? "uninstalling" : "installing");
                using (AssemblyInstaller inst = new AssemblyInstaller(typeof(Program).Assembly, args))
                {
                    IDictionary state = new Hashtable();
                    inst.UseNewContext = true;
                    try
                    {
                        if (undo)
                        {
                            inst.Uninstall(state);
                        }
                        else
                        {
                            inst.Install(state);
                            inst.Commit(state);
                            try
                            {
                                ServiceController service = new ServiceController("DpFamService");
                                TimeSpan timeout = TimeSpan.FromMilliseconds(1000);

                                service.Start();
                                service.WaitForStatus(ServiceControllerStatus.Running, timeout);

                            }
                            catch
                            {
                                Console.WriteLine("Could not start the server\n");
                            }

                        }
                    }
                    catch
                    {
                        try
                        {
                            inst.Rollback(state);
                        }
                        catch { }
                        throw;
                    }
                }
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine(ex.Message);
            }
        }
コード例 #32
0
        /// <summary>Checks to see if the specified assembly can be installed.</summary>
        /// <param name="assemblyName">The assembly in which to search for installers. </param>
        /// <exception cref="T:System.Exception">The specified assembly cannot be installed. </exception>
        public static void CheckIfInstallable(string assemblyName)
        {
            var assemblyInstaller = new AssemblyInstaller();

            assemblyInstaller.UseNewContext = false;
            assemblyInstaller.Path          = assemblyName;
            assemblyInstaller.CommandLine   = new string[0];
            assemblyInstaller.Context       = new InstallContext(null, new string[0]);
            assemblyInstaller.InitializeFromAssembly();
            if (assemblyInstaller.Installers.Count == 0)
            {
                throw new InvalidOperationException(Res.GetString("InstallNoPublicInstallers", assemblyName));
            }
        }
コード例 #33
0
        public static void CheckIfInstallable(string assemblyName)
        {
            AssemblyInstaller installer = new AssemblyInstaller {
                UseNewContext = false,
                Path          = assemblyName,
                CommandLine   = new string[0],
                Context       = new InstallContext(null, new string[0])
            };

            installer.InitializeFromAssembly();
            if (installer.Installers.Count == 0)
            {
                throw new InvalidOperationException(System.Configuration.Install.Res.GetString("InstallNoPublicInstallers", new object[] { assemblyName }));
            }
        }
コード例 #34
0
        /// <include file='doc\AssemblyInstaller.uex' path='docs/doc[@for="AssemblyInstaller.CheckIfInstallable"]/*' />
        /// <devdoc>
        ///    <para>
        ///       Finds the installers in the specified assembly, creates
        ///       a new instance of <see cref='System.Configuration.Install.AssemblyInstaller'/>
        ///       , and adds the installers to its installer collection.
        ///    </para>
        /// </devdoc>
        public static void CheckIfInstallable(string assemblyName)
        {
            AssemblyInstaller tester = new AssemblyInstaller();

            tester.UseNewContext = false;
            tester.Path          = assemblyName;
            tester.CommandLine   = new string[0];
            tester.Context       = new InstallContext(null, new string[0]);

            // this does the actual check and throws if necessary.
            tester.InitializeFromAssembly();
            if (tester.Installers.Count == 0)
            {
                throw new InvalidOperationException(Res.GetString(Res.InstallNoPublicInstallers, assemblyName));
            }
        }
コード例 #35
0
        public static string Uninstall(string filePath, string serviceName)
        {
            string msg = string.Empty;

            try
            {
                System.Configuration.Install.AssemblyInstaller myAssemblyInstaller = new System.Configuration.Install.AssemblyInstaller();
                myAssemblyInstaller.UseNewContext = true;
                myAssemblyInstaller.Path          = filePath;
                myAssemblyInstaller.Uninstall(null);
                myAssemblyInstaller.Dispose();
                msg = "卸载服务" + serviceName + "成功!";
            }
            catch (Exception er)
            {
                msg = er.ToString();
            }
            return(msg);
        }
コード例 #36
0
        // Run the installation process for a specific assembly.
        private void RunInstall(String filename)
        {
            // Load the installer assembly.
            AssemblyInstaller inst;

            inst = new AssemblyInstaller(filename, options);

            // Wrap the installer in a transaction.
            TransactedInstaller trans;

            trans = new TransactedInstaller();
            trans.Installers.Add(inst);

            // Install the assembly.
            IDictionary dict = new Hashtable();

            trans.Install(dict);

            // Write the state information, for later uninstall.
            // TODO
        }
コード例 #37
0
        // Run the uninstallation process for a specific assembly.
        private void RunUninstall(String filename)
        {
            // Load the installer assembly.
            AssemblyInstaller inst;

            inst = new AssemblyInstaller(filename, options);

            // Wrap the installer in a transaction.
            TransactedInstaller trans;

            trans = new TransactedInstaller();
            trans.Installers.Add(inst);

            // Load the previous state information from the install.
            IDictionary dict = new Hashtable();

            // TODO

            // Install the assembly.
            trans.Uninstall(dict);
        }
コード例 #38
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="exeFilename">Installer class (not the service class!)</param>
        /// <param name="exception"></param>
        /// <returns></returns>
        public static bool UninstallService(string exeFilename, out Exception exception)
        {
            exception = null;
            string[] commandLineOptions = new string[1] {
                "/LogFile=uninstall.log"
            };

            System.Configuration.Install.AssemblyInstaller installer =
                new System.Configuration.Install.AssemblyInstaller(exeFilename, commandLineOptions);

            try
            {
                installer.UseNewContext = true;
                installer.Uninstall(null);
            }
            catch (Exception ex)
            {
                exception = ex;
                return(false);
            }
            return(true);
        }
コード例 #39
0
 private void InitializeFromAssembly()
 {
     Type[] installerTypes;
     try
     {
         installerTypes = AssemblyInstaller.GetInstallerTypes(Assembly);
     }
     catch (Exception ex)
     {
         Context.LogMessage(Res.GetString("InstallException", (object)Path));
         Installer.LogException(ex, Context);
         Context.LogMessage(Res.GetString("InstallAbort", (object)Path));
         throw new InvalidOperationException(Res.GetString("InstallNoInstallerTypes", (object)Path), ex);
     }
     if (installerTypes == null || installerTypes.Length == 0)
     {
         Context.LogMessage(Res.GetString("InstallNoPublicInstallers", (object)Path));
     }
     else
     {
         for (var index = 0; index < installerTypes.Length; ++index)
         {
             try
             {
                 Installers.Add((Installer)Activator.CreateInstance(installerTypes[index], BindingFlags.Instance | BindingFlags.Public | BindingFlags.CreateInstance, null, new object[0], null));
             }
             catch (Exception ex)
             {
                 Context.LogMessage(Res.GetString("InstallCannotCreateInstance", (object)installerTypes[index].FullName));
                 Installer.LogException(ex, Context);
                 throw new InvalidOperationException(Res.GetString("InstallCannotCreateInstance", (object)installerTypes[index].FullName), ex);
             }
         }
         _initialized = true;
     }
 }
コード例 #40
0
        public static void InstallHelper(string[] args)
        {
            bool flag1 = false;
            bool flag2 = false;
            TransactedInstaller transactedInstaller = new TransactedInstaller();
            bool flag3 = false;

            try
            {
                ArrayList arrayList = new ArrayList();
                for (int index = 0; index < args.Length; ++index)
                {
                    if (args[index].StartsWith("/", StringComparison.Ordinal) || args[index].StartsWith("-", StringComparison.Ordinal))
                    {
                        string strA = args[index].Substring(1);
                        if (string.Compare(strA, "u", StringComparison.OrdinalIgnoreCase) == 0 || string.Compare(strA, "uninstall", StringComparison.OrdinalIgnoreCase) == 0)
                        {
                            flag1 = true;
                        }
                        else if (string.Compare(strA, "?", StringComparison.OrdinalIgnoreCase) == 0 || string.Compare(strA, "help", StringComparison.OrdinalIgnoreCase) == 0)
                        {
                            flag3 = true;
                        }
                        else if (string.Compare(strA, "AssemblyName", StringComparison.OrdinalIgnoreCase) == 0)
                        {
                            flag2 = true;
                        }
                        else
                        {
                            arrayList.Add((object)args[index]);
                        }
                    }
                    else
                    {
                        Assembly assembly;
                        try
                        {
                            assembly = !flag2?Assembly.LoadFrom(args[index]) : Assembly.Load(args[index]);
                        }
                        catch (Exception ex)
                        {
                            if (args[index].IndexOf('=') != -1)
                            {
                                throw new ArgumentException(Res.GetString("InstallFileDoesntExistCommandLine", new object[1]
                                {
                                    (object)args[index]
                                }), ex);
                            }
                            else
                            {
                                throw;
                            }
                        }
                        AssemblyInstaller assemblyInstaller = new AssemblyInstaller(assembly, (string[])arrayList.ToArray(typeof(string)));
                        transactedInstaller.Installers.Add((Installer)assemblyInstaller);
                    }
                }
                if (flag3 || transactedInstaller.Installers.Count == 0)
                {
                    flag3 = true;
                    transactedInstaller.Installers.Add((Installer) new AssemblyInstaller());
                    throw new InvalidOperationException(ManagedInstallerClass.GetHelp((Installer)transactedInstaller));
                }
                else
                {
                    transactedInstaller.Context = new InstallContext("InstallUtil.InstallLog", (string[])arrayList.ToArray(typeof(string)));
                }
            }
            catch (Exception ex)
            {
                if (flag3)
                {
                    throw ex;
                }
                throw new InvalidOperationException(Res.GetString("InstallInitializeException", (object)ex.GetType().FullName, (object)ex.Message));
            }
            try
            {
                string strA1 = transactedInstaller.Context.Parameters["installtype"];
                if (strA1 != null && string.Compare(strA1, "notransaction", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    string strA2 = transactedInstaller.Context.Parameters["action"];
                    if (strA2 != null && string.Compare(strA2, "rollback", StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        transactedInstaller.Context.LogMessage(Res.GetString("InstallRollbackNtRun"));
                        for (int index = 0; index < transactedInstaller.Installers.Count; ++index)
                        {
                            transactedInstaller.Installers[index].Rollback((IDictionary)null);
                        }
                    }
                    else if (strA2 != null && string.Compare(strA2, "commit", StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        transactedInstaller.Context.LogMessage(Res.GetString("InstallCommitNtRun"));
                        for (int index = 0; index < transactedInstaller.Installers.Count; ++index)
                        {
                            transactedInstaller.Installers[index].Commit((IDictionary)null);
                        }
                    }
                    else if (strA2 != null && string.Compare(strA2, "uninstall", StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        transactedInstaller.Context.LogMessage(Res.GetString("InstallUninstallNtRun"));
                        for (int index = 0; index < transactedInstaller.Installers.Count; ++index)
                        {
                            transactedInstaller.Installers[index].Uninstall((IDictionary)null);
                        }
                    }
                    else
                    {
                        transactedInstaller.Context.LogMessage(Res.GetString("InstallInstallNtRun"));
                        for (int index = 0; index < transactedInstaller.Installers.Count; ++index)
                        {
                            transactedInstaller.Installers[index].Install((IDictionary)null);
                        }
                    }
                }
                else if (!flag1)
                {
                    IDictionary stateSaver = (IDictionary) new Hashtable();
                    transactedInstaller.Install(stateSaver);
                }
                else
                {
                    transactedInstaller.Uninstall((IDictionary)null);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
コード例 #41
0
        /// <summary>Handles the functionality of the Installutil.exe (Installer Tool).</summary>
        /// <param name="args">The arguments passed to the Installer Tool.</param>
        public static void InstallHelper(string[] args)
        {
            var doUninstall         = false;
            var shouldLoadByName    = false;
            var transactedInstaller = new TransactedInstaller();
            var showHelp            = false;

            try
            {
                var arrayList = new ArrayList();
                foreach (var arg in args)
                {
                    if (arg.StartsWith("-", StringComparison.Ordinal))
                    {
                        var strA = arg.Substring(1);
                        if (string.Compare(strA, "u", StringComparison.OrdinalIgnoreCase) == 0 || string.Compare(strA, "uninstall", StringComparison.OrdinalIgnoreCase) == 0)
                        {
                            doUninstall = true;
                        }
                        else if (string.Compare(strA, "?", StringComparison.OrdinalIgnoreCase) == 0 || string.Compare(strA, "help", StringComparison.OrdinalIgnoreCase) == 0)
                        {
                            showHelp = true;
                        }
                        else if (string.Compare(strA, "AssemblyName", StringComparison.OrdinalIgnoreCase) == 0)
                        {
                            shouldLoadByName = true;
                        }
                        else
                        {
                            arrayList.Add(arg);
                        }
                    }
                    else
                    {
                        Assembly assembly;
                        try
                        {
                            assembly = shouldLoadByName ? Assembly.Load(arg) : Assembly.LoadFrom(arg);
                        }
                        catch (Exception innerException)
                        {
                            if (arg.IndexOf('=') != -1)
                            {
                                throw new ArgumentException(Res.GetString("InstallFileDoesntExistCommandLine", arg), innerException);
                            }
                            throw;
                        }
                        var value = new AssemblyInstaller(assembly, (string[])arrayList.ToArray(typeof(string)));
                        transactedInstaller.Installers.Add(value);
                    }
                }
                if (showHelp || transactedInstaller.Installers.Count == 0)
                {
                    showHelp = true;
                    transactedInstaller.Installers.Add(new AssemblyInstaller());
                    throw new InvalidOperationException(GetHelp(transactedInstaller));
                }
                transactedInstaller.Context = new InstallContext("InstallUtil.InstallLog", (string[])arrayList.ToArray(typeof(string)));
            }
            catch (Exception ex)
            {
                if (showHelp)
                {
                    throw ex;
                }
                throw new InvalidOperationException(Res.GetString("InstallInitializeException", ex.GetType().FullName, ex.Message));
            }
            var installType = transactedInstaller.Context.Parameters["installtype"];

            if (installType != null && string.Compare(installType, "notransaction", StringComparison.OrdinalIgnoreCase) == 0)
            {
                var action = transactedInstaller.Context.Parameters["action"];
                if (action != null && string.Compare(action, "rollback", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    transactedInstaller.Context.LogMessage(Res.GetString("InstallRollbackNtRun"));
                    for (var j = 0; j < transactedInstaller.Installers.Count; j++)
                    {
                        transactedInstaller.Installers[j].Rollback(null);
                    }
                }
                else if (action != null && string.Compare(action, "commit", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    transactedInstaller.Context.LogMessage(Res.GetString("InstallCommitNtRun"));
                    for (var k = 0; k < transactedInstaller.Installers.Count; k++)
                    {
                        transactedInstaller.Installers[k].Commit(null);
                    }
                }
                else if (action != null && string.Compare(action, "uninstall", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    transactedInstaller.Context.LogMessage(Res.GetString("InstallUninstallNtRun"));
                    for (var l = 0; l < transactedInstaller.Installers.Count; l++)
                    {
                        transactedInstaller.Installers[l].Uninstall(null);
                    }
                }
                else
                {
                    transactedInstaller.Context.LogMessage(Res.GetString("InstallInstallNtRun"));
                    for (var m = 0; m < transactedInstaller.Installers.Count; m++)
                    {
                        transactedInstaller.Installers[m].Install(null);
                    }
                }
            }
            else if (!doUninstall)
            {
                IDictionary stateSaver = new Hashtable();
                transactedInstaller.Install(stateSaver);
            }
            else
            {
                transactedInstaller.Uninstall(null);
            }
        }
コード例 #42
0
        /// <include file='doc\ManagedInstaller.uex' path='docs/doc[@for="ManagedInstallerClass.InstallHelper"]/*' />
        /// <devdoc>
        ///    <para>[To be supplied.]</para>
        /// </devdoc>
        public static void InstallHelper(string[] args)
        {
            bool uninstall      = false;
            bool isAssemblyName = false;
            TransactedInstaller topLevelInstaller = new TransactedInstaller();
            bool showHelp = false;

            try {
                /*
                 * StreamWriter stream = new StreamWriter("c:\\installutilargs.txt", true);
                 * stream.WriteLine("----------");
                 * for (int i = 0; i < args.Length; i++)
                 *  stream.WriteLine(args[i]);
                 * stream.Close();
                 */


                // strategy: Use a TransactedInstaller to manage the top-level installation work.
                // It will perform rollback/commit as necessary. Go through the assemblies on the
                // command line and add an AssemblyInstaller for each of them to the TransactedInstaller's
                // Installers collection.
                //
                // as we walk the parameters, we'll encounter either a filename or a
                // parameter. If we get to a filename, create an assembly installer with
                // all of the parameters we've seen _so far_. This way parameters can
                // be different for the different assemblies.

                ArrayList parameters = new ArrayList();
                for (int i = 0; i < args.Length; i++)
                {
                    if (args[i].StartsWith("/") || args[i].StartsWith("-"))
                    {
                        string str = args[i].Substring(1);
                        if (string.Compare(str, "u", true, CultureInfo.InvariantCulture) == 0 || string.Compare(str, "uninstall", true, CultureInfo.InvariantCulture) == 0)
                        {
                            uninstall = true;
                        }
                        else if (string.Compare(str, "?", true, CultureInfo.InvariantCulture) == 0 || string.Compare(str, "help", true, CultureInfo.InvariantCulture) == 0)
                        {
                            showHelp = true;
                        }
                        else if (string.Compare(str, "AssemblyName", true, CultureInfo.InvariantCulture) == 0)
                        {
                            isAssemblyName = true;
                        }
                        else
                        {
                            parameters.Add(args[i]);
                        }
                    }
                    else
                    {
                        Assembly asm = null;
                        try {
                            if (isAssemblyName)
                            {
                                asm = Assembly.Load(args[i]);
                            }
                            else
                            {
                                asm = Assembly.LoadFrom(args[i]);
                            }
                        }
                        catch (Exception e) {
                            if (args[i].IndexOf('=') != -1)
                            {
                                // probably a mistake where /key=value was written as key=value,
                                // try to explain that
                                throw new ArgumentException(Res.GetString(Res.InstallFileDoesntExistCommandLine, args[i]), e);
                            }
                            else
                            {
                                // the assembly.Load{From} gives a good descriptive error - pass it up
                                throw;
                            }
                        }

                        AssemblyInstaller installer = new AssemblyInstaller(asm, (string[])parameters.ToArray(typeof(string)));
                        topLevelInstaller.Installers.Add(installer);
                    }
                }

                if (showHelp || topLevelInstaller.Installers.Count == 0)
                {
                    // we may have seen some options, but they didn't tell us to do any
                    // work. Or they gave us /? or /help. Show the help screen.
                    showHelp = true;
                    topLevelInstaller.Installers.Add(new AssemblyInstaller());
                    throw new InvalidOperationException(GetHelp(topLevelInstaller));
                }

                topLevelInstaller.Context = new InstallContext("InstallUtil.InstallLog", (string[])parameters.ToArray(typeof(string)));
            }
            catch (Exception e) {
                if (showHelp)
                {
                    // it's just the help message
                    throw e;
                }
                else
                {
                    throw new InvalidOperationException(Res.GetString(Res.InstallInitializeException, e.GetType().FullName, e.Message));
                }
            }

            try {
                // MSI mode.
                // If the parameter /installtype=notransaction is specified, then we don't want to run
                // a TransactedInstaller. Instead, we just use that installer as a container for all
                // the AssemblyInstallers we want to run.
                string installType = topLevelInstaller.Context.Parameters["installtype"];
                if (installType != null && string.Compare(installType, "notransaction", true, CultureInfo.InvariantCulture) == 0)
                {
                    // this is a non-transacted install. Check the value of the Action parameter
                    // to see what to do
                    string action = topLevelInstaller.Context.Parameters["action"];
                    if (action != null && string.Compare(action, "rollback", true, CultureInfo.InvariantCulture) == 0)
                    {
                        topLevelInstaller.Context.LogMessage(Res.GetString(Res.InstallRollbackNtRun));
                        for (int i = 0; i < topLevelInstaller.Installers.Count; i++)
                        {
                            topLevelInstaller.Installers[i].Rollback(null);
                        }
                        return;
                    }
                    if (action != null && string.Compare(action, "commit", true, CultureInfo.InvariantCulture) == 0)
                    {
                        topLevelInstaller.Context.LogMessage(Res.GetString(Res.InstallCommitNtRun));
                        for (int i = 0; i < topLevelInstaller.Installers.Count; i++)
                        {
                            topLevelInstaller.Installers[i].Commit(null);
                        }
                        return;
                    }
                    if (action != null && string.Compare(action, "uninstall", true, CultureInfo.InvariantCulture) == 0)
                    {
                        topLevelInstaller.Context.LogMessage(Res.GetString(Res.InstallUninstallNtRun));
                        for (int i = 0; i < topLevelInstaller.Installers.Count; i++)
                        {
                            topLevelInstaller.Installers[i].Uninstall(null);
                        }
                        return;
                    }
                    // they said notransaction, and they didn't tell us to do rollback, commit,
                    // or uninstall. They must mean install.
                    topLevelInstaller.Context.LogMessage(Res.GetString(Res.InstallInstallNtRun));
                    for (int i = 0; i < topLevelInstaller.Installers.Count; i++)
                    {
                        topLevelInstaller.Installers[i].Install(null);
                    }
                    return;
                }

                // transacted mode - we'll only get here if /installtype=notransaction wasn't specified.
                if (!uninstall)
                {
                    IDictionary stateSaver = new Hashtable();
                    topLevelInstaller.Install(stateSaver);
                    // we don't bother writing out the saved state for this guy, because each assembly
                    // we're installing gets its own saved-state file.
                }
                else
                {
                    topLevelInstaller.Uninstall(null);
                }
            }

            catch (Exception e) {
                /*
                 * StreamWriter stream = new StreamWriter("c:\\installutilargs.txt", true);
                 * stream.WriteLine("Caught exception: " + e.GetType().FullName + ": " + e.Message);
                 * stream.WriteLine(e.StackTrace);
                 * stream.Close();
                 */

                throw e;
            }

            /*
             * StreamWriter stream2 = new StreamWriter("c:\\installutilargs.txt", true);
             * stream2.WriteLine("Caught no exceptions. Returning 0.");
             * stream2.Close();
             */

            return;
        }
コード例 #43
0
        public static void InstallHelper(string[] args)
        {
            bool flag  = false;
            bool flag2 = false;
            TransactedInstaller installerWithHelp = new TransactedInstaller();
            bool flag3 = false;

            try
            {
                ArrayList list = new ArrayList();
                for (int i = 0; i < args.Length; i++)
                {
                    if (args[i].StartsWith("/", StringComparison.Ordinal) || args[i].StartsWith("-", StringComparison.Ordinal))
                    {
                        string strA = args[i].Substring(1);
                        if ((string.Compare(strA, "u", StringComparison.OrdinalIgnoreCase) == 0) || (string.Compare(strA, "uninstall", StringComparison.OrdinalIgnoreCase) == 0))
                        {
                            flag = true;
                        }
                        else if ((string.Compare(strA, "?", StringComparison.OrdinalIgnoreCase) == 0) || (string.Compare(strA, "help", StringComparison.OrdinalIgnoreCase) == 0))
                        {
                            flag3 = true;
                        }
                        else if (string.Compare(strA, "AssemblyName", StringComparison.OrdinalIgnoreCase) == 0)
                        {
                            flag2 = true;
                        }
                        else
                        {
                            list.Add(args[i]);
                        }
                    }
                    else
                    {
                        Assembly assembly = null;
                        try
                        {
                            if (flag2)
                            {
                                assembly = Assembly.Load(args[i]);
                            }
                            else
                            {
                                assembly = Assembly.LoadFrom(args[i]);
                            }
                        }
                        catch (Exception exception)
                        {
                            if (args[i].IndexOf('=') != -1)
                            {
                                throw new ArgumentException(Res.GetString("InstallFileDoesntExistCommandLine", new object[] { args[i] }), exception);
                            }
                            throw;
                        }
                        AssemblyInstaller installer2 = new AssemblyInstaller(assembly, (string[])list.ToArray(typeof(string)));
                        installerWithHelp.Installers.Add(installer2);
                    }
                }
                if (flag3 || (installerWithHelp.Installers.Count == 0))
                {
                    flag3 = true;
                    installerWithHelp.Installers.Add(new AssemblyInstaller());
                    throw new InvalidOperationException(GetHelp(installerWithHelp));
                }
                installerWithHelp.Context = new InstallContext("InstallUtil.InstallLog", (string[])list.ToArray(typeof(string)));
            }
            catch (Exception exception2)
            {
                if (flag3)
                {
                    throw exception2;
                }
                throw new InvalidOperationException(Res.GetString("InstallInitializeException", new object[] { exception2.GetType().FullName, exception2.Message }));
            }
            try
            {
                string str2 = installerWithHelp.Context.Parameters["installtype"];
                if ((str2 != null) && (string.Compare(str2, "notransaction", StringComparison.OrdinalIgnoreCase) == 0))
                {
                    string str3 = installerWithHelp.Context.Parameters["action"];
                    if ((str3 != null) && (string.Compare(str3, "rollback", StringComparison.OrdinalIgnoreCase) == 0))
                    {
                        installerWithHelp.Context.LogMessage(Res.GetString("InstallRollbackNtRun"));
                        for (int j = 0; j < installerWithHelp.Installers.Count; j++)
                        {
                            installerWithHelp.Installers[j].Rollback(null);
                        }
                    }
                    else if ((str3 != null) && (string.Compare(str3, "commit", StringComparison.OrdinalIgnoreCase) == 0))
                    {
                        installerWithHelp.Context.LogMessage(Res.GetString("InstallCommitNtRun"));
                        for (int k = 0; k < installerWithHelp.Installers.Count; k++)
                        {
                            installerWithHelp.Installers[k].Commit(null);
                        }
                    }
                    else if ((str3 != null) && (string.Compare(str3, "uninstall", StringComparison.OrdinalIgnoreCase) == 0))
                    {
                        installerWithHelp.Context.LogMessage(Res.GetString("InstallUninstallNtRun"));
                        for (int m = 0; m < installerWithHelp.Installers.Count; m++)
                        {
                            installerWithHelp.Installers[m].Uninstall(null);
                        }
                    }
                    else
                    {
                        installerWithHelp.Context.LogMessage(Res.GetString("InstallInstallNtRun"));
                        for (int n = 0; n < installerWithHelp.Installers.Count; n++)
                        {
                            installerWithHelp.Installers[n].Install(null);
                        }
                    }
                }
                else if (!flag)
                {
                    IDictionary stateSaver = new Hashtable();
                    installerWithHelp.Install(stateSaver);
                }
                else
                {
                    installerWithHelp.Uninstall(null);
                }
            }
            catch (Exception exception3)
            {
                throw exception3;
            }
        }