Skip to content

Commit 7430243

Browse files
committed
Add project files.
1 parent 4af2792 commit 7430243

File tree

4 files changed

+146
-0
lines changed

4 files changed

+146
-0
lines changed

install-edge.sln

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.0.31903.59
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "install-edge", "install-edge\install-edge.csproj", "{37ECED83-8B84-40FE-BD10-F66CA25A763A}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{37ECED83-8B84-40FE-BD10-F66CA25A763A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{37ECED83-8B84-40FE-BD10-F66CA25A763A}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{37ECED83-8B84-40FE-BD10-F66CA25A763A}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{37ECED83-8B84-40FE-BD10-F66CA25A763A}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {5C0F745F-7128-4DED-A29D-9BD65B7EF101}
24+
EndGlobalSection
25+
EndGlobal

install-edge/Downloader.cs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
public class Downloader : IDisposable {
2+
private readonly string _downloadUrl;
3+
private readonly string _destinationFilePath;
4+
5+
private HttpClient _httpClient = new HttpClient { Timeout = TimeSpan.FromDays(1) };
6+
7+
public delegate void ProgressChangedHandler(long? totalFileSize, long totalBytesDownloaded, double? progressPercentage);
8+
9+
public event ProgressChangedHandler? ProgressChanged;
10+
11+
public Downloader(string downloadUrl, string destinationFilePath) {
12+
_downloadUrl = downloadUrl;
13+
_destinationFilePath = destinationFilePath;
14+
}
15+
16+
public async Task StartDownload() {
17+
//_httpClient = new HttpClient { Timeout = TimeSpan.FromDays(1) };
18+
19+
using (var response = await _httpClient.GetAsync(_downloadUrl, HttpCompletionOption.ResponseHeadersRead))
20+
await DownloadFileFromHttpResponseMessage(response);
21+
}
22+
23+
private async Task DownloadFileFromHttpResponseMessage(HttpResponseMessage response) {
24+
response.EnsureSuccessStatusCode();
25+
26+
var totalBytes = response.Content.Headers.ContentLength;
27+
28+
using (var contentStream = await response.Content.ReadAsStreamAsync())
29+
await ProcessContentStream(totalBytes, contentStream);
30+
}
31+
32+
private async Task ProcessContentStream(long? totalDownloadSize, Stream contentStream) {
33+
var totalBytesRead = 0L;
34+
var readCount = 0L;
35+
var buffer = new byte[8192];
36+
var isMoreToRead = true;
37+
38+
using (var fileStream = new FileStream(_destinationFilePath, FileMode.Create, FileAccess.Write, FileShare.None, 8192, true)) {
39+
do {
40+
var bytesRead = await contentStream.ReadAsync(buffer, 0, buffer.Length);
41+
if (bytesRead == 0) {
42+
isMoreToRead = false;
43+
TriggerProgressChanged(totalDownloadSize, totalBytesRead);
44+
continue;
45+
}
46+
47+
await fileStream.WriteAsync(buffer, 0, bytesRead);
48+
49+
totalBytesRead += bytesRead;
50+
readCount += 1;
51+
52+
if (readCount % 100 == 0)
53+
TriggerProgressChanged(totalDownloadSize, totalBytesRead);
54+
}
55+
while (isMoreToRead);
56+
}
57+
}
58+
59+
private void TriggerProgressChanged(long? totalDownloadSize, long totalBytesRead) {
60+
if (ProgressChanged == null)
61+
return;
62+
63+
double? progressPercentage = null;
64+
if (totalDownloadSize.HasValue)
65+
progressPercentage = Math.Round((double)totalBytesRead / totalDownloadSize.Value * 100, 2);
66+
67+
ProgressChanged(totalDownloadSize, totalBytesRead, progressPercentage);
68+
}
69+
70+
public void Dispose() {
71+
_httpClient?.Dispose();
72+
}
73+
}

install-edge/Program.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+

2+
3+
using System.Diagnostics;
4+
5+
int width = Console.WindowWidth;
6+
double? lastProgress = 0.0;
7+
8+
string edgeUrl = @"https://msedge.sf.dl.delivery.mp.microsoft.com/filestreamingservice/files/a2662b5b-97d0-4312-8946-598355851b3b/MicrosoftEdgeEnterpriseX64.msi";
9+
string downloadPath = Path.Combine(Path.GetTempPath(), "automated-edge-download", "MicrosoftEdgeEnterpriseX64.msi");
10+
11+
if (!Directory.Exists(Path.GetDirectoryName(downloadPath))) { Directory.CreateDirectory(Path.GetDirectoryName(downloadPath)); }
12+
if (File.Exists(downloadPath)) { File.Delete(downloadPath); }
13+
14+
using (Downloader downloader = new Downloader(edgeUrl, downloadPath)) {
15+
downloader.ProgressChanged += (totalBytes, downloadedBytes, percentComplete) => {
16+
if (lastProgress <= percentComplete - 10.0) {
17+
Console.WriteLine($"downloaded {downloadedBytes} ({percentComplete}%)");
18+
lastProgress = percentComplete;
19+
}
20+
};
21+
22+
Console.WriteLine("Beginning download...");
23+
await downloader.StartDownload();
24+
Console.WriteLine("Download complete. Beginning install...");
25+
26+
Process edgeInstaller = Process.Start(new ProcessStartInfo { FileName = downloadPath, UseShellExecute = true });
27+
if (edgeInstaller != null) {
28+
edgeInstaller.Exited += (s, e) => {
29+
Console.WriteLine("Cleaning up...");
30+
File.Delete(downloadPath);
31+
Directory.Delete(Path.GetDirectoryName(downloadPath), true);
32+
};
33+
34+
edgeInstaller.WaitForExit();
35+
}
36+
Console.WriteLine("done.");
37+
}

install-edge/install-edge.csproj

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0-windows</TargetFramework>
6+
<RootNamespace>install_edge</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
</Project>

0 commit comments

Comments
 (0)