/// <summary> /// Starts the updater, it will wait for the given process (defaults to current process) to end before starting its business. /// </summary> /// <param name="pidToWait"></param> /// <param name="delayBeforeActionInMilliseconds"></param> public void Start(int?pidToWait = null, int?delayBeforeActionInMilliseconds = null) { if (!Directory.Exists(_exeDirectoryPath)) { Directory.CreateDirectory(_exeDirectoryPath); } var executablePath = Resources.Resources.WriteSimpleFileUpdateFile(DotNet.IsNetStandardBuild, _requireAdminExe, _exeDirectoryPath); var actionFilePath = Path.Combine(_exeDirectoryPath, Path.GetRandomFileName()); File.WriteAllText(actionFilePath, _output.ToString(), Encoding.Default); _process = new Process { StartInfo = { FileName = DotNet.IsNetStandardBuild ? DotNet.FullPathOrDefault() : _exeDirectoryPath, Arguments = $"{(DotNet.IsNetStandardBuild ? $"{Path.GetFileName(executablePath)} " : "")}--pid {pidToWait ?? Process.GetCurrentProcess().Id} --action-file \"{actionFilePath}\"{(delayBeforeActionInMilliseconds != null ? $" --wait {delayBeforeActionInMilliseconds}" : "")}", WindowStyle = ProcessWindowStyle.Hidden, UseShellExecute = !DotNet.IsNetStandardBuild, WorkingDirectory = _exeDirectoryPath, Verb = _requireAdminExe ? "runas" : "" } }; _process.Start(); }
/// <summary> /// Starts the updater, it will wait for the given process (defaults to current process) to end before starting its business. /// </summary> /// <param name="pidToWait"></param> /// <param name="delayBeforeActionInMilliseconds"></param> public void Start(int?pidToWait = null, int?delayBeforeActionInMilliseconds = null) { if (_process != null) { try { _process?.Kill(); _process?.WaitForExit(); _process?.Dispose(); if (File.Exists(_actionFilePath)) { File.Delete(_actionFilePath); } } catch (Exception) { // ignored } _process = null; } var exeDirectoryPath = ExeDirectoryPath; if (!Directory.Exists(exeDirectoryPath)) { Directory.CreateDirectory(exeDirectoryPath); } var executablePath = Resources.Resources.WriteSimpleFileUpdateFile(DotNet.IsNetStandardBuild, IsAdminRightsNeeded, exeDirectoryPath); _actionFilePath = Path.Combine(exeDirectoryPath, Path.GetRandomFileName()); File.WriteAllText(_actionFilePath, _output.ToString(), Encoding.Default); _process = new Process { StartInfo = { FileName = DotNet.IsNetStandardBuild ? DotNet.FullPathOrDefault() : executablePath, Arguments = $"{(DotNet.IsNetStandardBuild ? $"{Path.GetFileName(executablePath)} " : "")}--pid {pidToWait ?? Process.GetCurrentProcess().Id} --action-file \"{_actionFilePath}\"{(delayBeforeActionInMilliseconds != null ? $" --wait {delayBeforeActionInMilliseconds}" : "")}", WindowStyle = ProcessWindowStyle.Hidden, UseShellExecute = !DotNet.IsNetStandardBuild, WorkingDirectory = exeDirectoryPath, Verb = IsAdminRightsNeeded ? "runas" : "" } }; _process.Start(); }