Skip to content

Commit

Permalink
Optimze ProcessStartInfo arguments handling (#616). This fixes #608.
Browse files Browse the repository at this point in the history
  • Loading branch information
fishjimi authored Apr 27, 2023
1 parent 5a9ad53 commit 68af9fc
Showing 1 changed file with 27 additions and 26 deletions.
53 changes: 27 additions & 26 deletions AutoUpdater.NET/DownloadUpdateDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mime;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;
using AutoUpdaterDotNET.Properties;

Expand Down Expand Up @@ -69,7 +69,7 @@ private void OnDownloadProgressChanged(object sender, DownloadProgressChangedEve
else
{
var timeSpan = DateTime.Now - _startedAt;
long totalSeconds = (long) timeSpan.TotalSeconds;
long totalSeconds = (long)timeSpan.TotalSeconds;
if (totalSeconds > 0)
{
var bytesPerSecond = e.BytesReceived / totalSeconds;
Expand Down Expand Up @@ -176,49 +176,50 @@ private void WebClientOnDownloadFileCompleted(object sender, AsyncCompletedEvent
extractionPath = AutoUpdater.InstallationPath;
}

StringBuilder arguments =
new StringBuilder(
$"--input \"{tempPath}\" --output \"{extractionPath}\" --current-exe \"{currentExe}\"");
processStartInfo = new ProcessStartInfo
{
FileName = installerPath,
UseShellExecute = true
};

processStartInfo.ArgumentList.Add("--input");
processStartInfo.ArgumentList.Add(tempPath);
processStartInfo.ArgumentList.Add("--output");
processStartInfo.ArgumentList.Add(extractionPath);
processStartInfo.ArgumentList.Add("--current-exe");
processStartInfo.ArgumentList.Add(currentExe);

if (!string.IsNullOrWhiteSpace(updatedExe))
{
arguments.Append($" --updated-exe \"{updatedExe}\"");
processStartInfo.ArgumentList.Add("--updated-exe");
processStartInfo.ArgumentList.Add(updatedExe);
}

if (AutoUpdater.ClearAppDirectory)
{
arguments.Append(" --clear");
processStartInfo.ArgumentList.Add("--clear");
}

string[] args = Environment.GetCommandLineArgs();
for (int i = 1; i < args.Length; i++)
if (args.Length > 0)
{
if (i.Equals(1))
{
arguments.Append(" --args \"");
}
var arguments = string.Join(" ", args.Skip(1).Select(arg => $"\"{arg}\""));
processStartInfo.ArgumentList.Add("--args");
processStartInfo.ArgumentList.Add(arguments);

arguments.Append($"\"{args[i]}\"");
arguments.Append(i.Equals(args.Length - 1) ? "\"" : " ");
}

processStartInfo = new ProcessStartInfo
{
FileName = installerPath,
UseShellExecute = true,
Arguments = arguments.ToString()
};
}
else if (extension.Equals(".msi", StringComparison.OrdinalIgnoreCase))
{
processStartInfo = new ProcessStartInfo
{
FileName = "msiexec",
Arguments = $"/i \"{tempPath}\"",
FileName = "msiexec"
};
processStartInfo.ArgumentList.Add($"/i");
processStartInfo.ArgumentList.Add(tempPath);
if (!string.IsNullOrEmpty(installerArgs))
{
processStartInfo.Arguments += " " + installerArgs;
processStartInfo.ArgumentList.Add(installerArgs);
}
}

Expand Down Expand Up @@ -258,7 +259,7 @@ private void WebClientOnDownloadFileCompleted(object sender, AsyncCompletedEvent

private static string BytesToString(long byteCount)
{
string[] suf = {"B", "KB", "MB", "GB", "TB", "PB", "EB"};
string[] suf = { "B", "KB", "MB", "GB", "TB", "PB", "EB" };
if (byteCount == 0)
return "0" + suf[0];
long bytes = Math.Abs(byteCount);
Expand Down Expand Up @@ -291,7 +292,7 @@ private void DownloadUpdateDialog_FormClosing(object sender, FormClosingEventArg
AutoUpdater.Exit();
return;
}
if (_webClient is {IsBusy: true})
if (_webClient is { IsBusy: true })
{
_webClient.CancelAsync();
DialogResult = DialogResult.Cancel;
Expand Down

0 comments on commit 68af9fc

Please sign in to comment.