Skip to content

Commit

Permalink
Utlize WebClient
Browse files Browse the repository at this point in the history
* Use WebClient instead of HttpClient in order to handle other file requests like FTP
  • Loading branch information
rex706 committed Jan 8, 2021
1 parent 22f9bf4 commit 003faa3
Showing 1 changed file with 48 additions and 45 deletions.
93 changes: 48 additions & 45 deletions Updater/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Windows;

Expand Down Expand Up @@ -32,7 +31,7 @@ public MainWindow()
InitializeComponent();
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
this.Loaded += MainWindow_LoadedAsync;
Loaded += MainWindow_LoadedAsync;
}

private async void MainWindow_LoadedAsync(object sender, RoutedEventArgs e)
Expand All @@ -53,64 +52,68 @@ private async void MainWindow_LoadedAsync(object sender, RoutedEventArgs e)
ConsoleBox.AppendText("Reading update manifest . . .\n");

// Open the text file using a stream reader.
using (Stream stream = await new HttpClient().GetStreamAsync(manifestUrl))
using (WebClient client = new WebClient())
{
StreamReader reader = new StreamReader(stream);

// Initialize variables.
Version latest = null;
string executable = null;

try
// Open the text file using a stream reader.
using (Stream stream = await client.OpenReadTaskAsync(manifestUrl))
{
// First two expected items in manifest will be the new version and the starting executable.
latest = Version.Parse(await reader.ReadLineAsync());
executable = await reader.ReadLineAsync();
}
catch (Exception m)
{
MessageBox.Show("Error parsing new version number or launch executable.");
Close();
}
StreamReader reader = new StreamReader(stream);

// Initialize variables.
Version latest = null;
string executable = null;

// Load manifest urls and file names.
while (!reader.EndOfStream)
{
try
{
// Expected in pairs of two.
string downloadUrl = await reader.ReadLineAsync();
string downloadFile = await reader.ReadLineAsync();
if (downloadUrl == null || downloadFile == null)
{
MessageBox.Show("Uneven update manifest.\nPlease review format.");
Close();
}
// First two expected items in manifest will be the new version and the starting executable.
latest = Version.Parse(await reader.ReadLineAsync());
executable = await reader.ReadLineAsync();
}
catch (Exception m)
{
MessageBox.Show("Error parsing new version number or launch executable.");
Close();
}

// If a new version of the updater is being acquired, rename it to be handled in UpdateCheck.cs
if (downloadFile == "Updater.exe")
// Load manifest urls and file names.
while (!reader.EndOfStream)
{
try
{
arguments.Add(new Download { url = downloadUrl, file = "Updater_new.exe" });
// Expected in pairs of two.
string downloadUrl = await reader.ReadLineAsync();
string downloadFile = await reader.ReadLineAsync();

if (downloadUrl == null || downloadFile == null)
{
MessageBox.Show("Uneven update manifest.\nPlease review format.");
Close();
}

// If a new version of the updater is being acquired, rename it to be handled in UpdateCheck.cs
if (downloadFile == "Updater.exe")
{
arguments.Add(new Download { url = downloadUrl, file = "Updater_new.exe" });
}
else
{
arguments.Add(new Download { url = downloadUrl, file = downloadFile });
}
}
else
catch (Exception m)
{
arguments.Add(new Download { url = downloadUrl, file = downloadFile });
MessageBox.Show("Uneven update manifest.\nPlease review format.");
Close();
}
}
catch (Exception m)
if (arguments.Count == 0)
{
MessageBox.Show("Uneven update manifest.\nPlease review format.");
MessageBox.Show("No download arguments found in manifest.");
Close();
}
}
if (arguments.Count == 0)
{
MessageBox.Show("No download arguments found in manifest.");
Close();
}

Update(executable, arguments);
Update(executable, arguments);
}
}
}

Expand Down

0 comments on commit 003faa3

Please sign in to comment.