Beispiel #1
0
        static void Main(string[] args)
        {
            var logger = new FileLogger("InstallationLog.txt");

            //SeviceName = args[0];
            //var parameter = string.Concat(args).Substring(SeviceName.Length);

            try
            {
                using (var serviceHelper = new ServiceHelper(SeviceName, "TwainWeb.Standalone.exe"))
                {
                    var parameter = string.Concat(args);
                    switch (parameter)
                    {
                    case "-install":
                        if (serviceHelper.Install())
                        {
                            logger.Info("Start install service...");
                            return;
                        }
                        else
                        {
                            logger.Error("NOT Start install service...");
                        }

                        Environment.Exit(-1);
                        break;

                    case "-uninstall":
                        if (serviceHelper.Uninstall())
                        {
                            return;
                        }

                        Environment.Exit(-1);
                        break;

                    case "-start":
                        serviceHelper.Start();
                        return;

                    case "-stop":
                        serviceHelper.Stop();
                        return;

                    case "-restart":
                        serviceHelper.Restart();
                        return;

                    case "-run-uninstaller":

                        try
                        {
                            var uninstallString = GetUninstallString();

                            if (File.Exists(uninstallString))
                            {
                                var process = new Process {
                                    StartInfo = { FileName = uninstallString }
                                };
                                process.Start();
                                process.WaitForExit();

                                var pr = Process.GetProcessesByName("appun-1");
                                if (pr.Length > 0)
                                {
                                    pr[0].WaitForExit();
                                    var exitCode = File.Exists(uninstallString) ? 1 : 0;
                                    Environment.Exit(exitCode);
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            logger.Error("Error occured while run uninstaller: " + e);
                        }

                        return;
                    }
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
        }
Beispiel #2
0
        internal bool Install()
        {
            _logger.Info(string.Format("Installation windows service '{0}'...", _serviceName));

            if (_isInstalled)
            {
                _logger.Info("Need to uninstall service");
                var success = Uninstall();
                if (!success)
                {
                    _logger.Error("Installation failed");
                    return(false);
                }
            }

            var exeFile = Path.Combine(AssemblyDirectory, _serviceFilename);

            if (!File.Exists(exeFile))
            {
                _logger.Error(string.Format("File '{0}' not found", exeFile));
                MessageBox.Show(string.Format("Не удалось найти исполняемый файл: {0}.", exeFile));
                return(false);
            }
            ManagedInstallerClass.InstallHelper(new[] { exeFile });
            SetRecoveryOptions(_serviceName);

            _isInstalled = true;
            _logger.Info(string.Format("Installation of windows service '{0}' completed successfully", _serviceName));
            return(true);
        }