본문 바로가기
C#

[C#] .NET 5 설치 여부 확인 후 자동 설치

by Jcoder 2021. 8. 31.

.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