.NET 5 대상으로 한 프로그램에서 실행이 안되기 때문에 .NET Framework 4.8 대상으로 프로젝트 빌드함.
64비트 os인지 확인
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace UpdateProgram
{
public static class InstallHelper
{
public static bool IsInstallNET5() => System.IO.File.Exists(@"C:\Program Files\dotnet\dotnet.exe");
public static async Task InstallNET5Async()
{
string net5x64InstallLink = "https://download.visualstudio.microsoft.com/download/pr/8bc41df1-cbb4-4da6-944f-6652378e9196/1014aacedc80bbcc030dabb168d2532f/windowsdesktop-runtime-5.0.9-win-x64.exe";
string net5x64InstallFilename = System.IO.Path.Combine(Application.StartupPath, "windowsdesktop-runtime-5.0.9-win-x64.exe");
string net5x86InstallLink = "https://download.visualstudio.microsoft.com/download/pr/334f5618-b0fa-474c-b55e-1d10c9142161/61eb66bf79d0e6cf36f894a5fe847634/dotnet-runtime-5.0.9-win-x86.exe";
string net5x86InstallFilename = System.IO.Path.Combine(Application.StartupPath, "dotnet-runtime-5.0.9-win-x86.exe");
if (!IsInstallNET5())
{
using (var webClient = new System.Net.WebClient())
{
bool isDownload = false;
try
{
webClient.DownloadFileCompleted += (s, e) => { isDownload = true; };
if (Environment.Is64BitOperatingSystem)
await webClient.DownloadFileTaskAsync(net5x64InstallLink, net5x64InstallFilename);
else
await webClient.DownloadFileTaskAsync(net5x86InstallLink, net5x86InstallFilename);
if (isDownload && (System.IO.File.Exists(net5x64InstallFilename) || System.IO.File.Exists(net5x86InstallFilename)))
{
using (var process = new System.Diagnostics.Process())
{
process.StartInfo.FileName = Environment.Is64BitOperatingSystem ? net5x64InstallFilename : net5x86InstallFilename;
process.StartInfo.Arguments = "/install /quiet /norestart";
process.Start();
process.WaitForExit();
process.Close();
}
if (IsInstallNET5())
{
if (Environment.Is64BitOperatingSystem)
System.IO.File.Delete(net5x64InstallFilename);
else
System.IO.File.Delete(net5x86InstallFilename);
}
}
}
catch (Exception e)
{
}
}
}
}
}
}
Windows, Linux, macOS에 설치된 .NET 버전 확인 - .NET | Microsoft Docs
'C#' 카테고리의 다른 글
[C#] dll 코드로 등록 방법 (관리자 권한으로 실행해야 함) (0) | 2021.08.31 |
---|---|
[C#] Webview2 runtime 설치 여부 및 설치 (0) | 2021.08.31 |
[C#] yield return 사용해야 할 때와 동기, 비동기 사용 방법 (0) | 2021.08.16 |
[C#] WinUI3 소개 (0) | 2021.08.16 |
[C#] Get HttpStatusCode Message (0) | 2021.08.02 |