Exemplo n.º 1
0
        public static void Main(string[] args)
        {
            Console.Title = "Server Manager Updater";

            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

            var updaterArgs = args;

            ApplicationArgs = null;
            DownloadUrl     = null;
            Prefix          = null;

            try
            {
                // check if the updater is already running
                if (ProcessUtils.IsAlreadyRunning())
                {
                    throw new Exception("The updater is already running.");
                }

                var process = Validate(updaterArgs);
                CloseApplication(process);
                process = null;

                Update();
                RestartApplication();

                Environment.ExitCode = 0;
            }
            catch (Exception ex)
            {
                OutputError(ex.Message, ex.StackTrace);
                Environment.ExitCode = 1;

                OutputMessage("Press any key to continue.");
                Console.ReadKey(true);
            }
        }
Exemplo n.º 2
0
        private static Process Validate(string[] args)
        {
            OutputMessage("Validating update...");

            // argument format - PID, DownloadUrl, Prefix
            if (args == null)
            {
                throw new ArgumentNullException(nameof(args), "The arguments cannot be null or empty");
            }
            if (args == null || args.Length != 3)
            {
                throw new ArgumentOutOfRangeException(nameof(args), "The arguments do not contain valid information.");
            }

            // check if the passed pid is valid
            if (!int.TryParse(args[0], out int pid))
            {
                throw new InvalidCastException("The process id value is not valid.");
            }

            DownloadUrl = args[1];
            if (string.IsNullOrWhiteSpace(DownloadUrl))
            {
                throw new ArgumentNullException(nameof(DownloadUrl), "The download url cannot be null or empty.");
            }

            Prefix = args[2];
            if (string.IsNullOrWhiteSpace(Prefix))
            {
                throw new ArgumentNullException(nameof(Prefix), "The prefix cannot be null or empty.");
            }

            // get the process associated with the pid
            var process = ProcessUtils.GetProcess(pid);

            if (process == null)
            {
                throw new Exception("The process id value is not associated with a running application.");
            }

            // get a list of the running processes with the same name and file location
            var executablePath = ProcessUtils.GetMainModuleFilepath(pid);
            var processes      = ProcessUtils.GetProcesses(process.ProcessName, executablePath);

            // check if there is more than one instance of the application running
            if (processes.Length != 1)
            {
                throw new Exception("The application to be updated has more than one instance running.");
            }

            // get the command line of the process
            var commandLine = ProcessUtils.GetCommandLineForProcess(pid);

            ApplicationArgs = ProcessUtils.CommandLineToArgs(commandLine);

            if (ApplicationArgs == null || ApplicationArgs.Length == 0)
            {
                throw new ArgumentNullException(nameof(ApplicationArgs), "The application args cannot be null or empty.");
            }

            return(process);
        }