Skip to content

Instantly share code, notes, and snippets.

@brandon15811
Created May 27, 2023 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brandon15811/7426173e7daeb78ca3fa06939b8a1934 to your computer and use it in GitHub Desktop.
Save brandon15811/7426173e7daeb78ca3fa06939b8a1934 to your computer and use it in GitHub Desktop.
Install Winget without Powershell
using System.Collections;
using System.Net;
using System.IO.Compression;
using Windows.Management.Deployment;
namespace MSIXInstaller
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("MSIX Installer");
try
{
// Download and install the Microsoft.VCLibs.x64.14.00.Desktop.appx package
Console.WriteLine("Downloading Microsoft.VCLibs.x64.14.00.Desktop.appx...");
DownloadFile("https://aka.ms/Microsoft.VCLibs.x64.14.00.Desktop.appx", "Microsoft.VCLibs.x64.14.00.Desktop.appx");
InstallAppxPackage("Microsoft.VCLibs.x64.14.00.Desktop.appx");
// Step 2: Download and extract Microsoft.UI.Xaml package
string xamlPackageUrl = "https://www.nuget.org/api/v2/package/Microsoft.UI.Xaml/2.7.3";
string xamlZipFilePath = "./microsoft.ui.xaml.2.7.3.zip";
string extractedFolderPath = "./microsoft.ui.xaml.2.7.3";
Console.WriteLine($"Downloading {xamlPackageUrl}...");
DownloadFile(xamlPackageUrl, xamlZipFilePath);
Console.WriteLine("Extracting the package...");
ExtractZipFile(xamlZipFilePath, extractedFolderPath);
// Step 3: Install Microsoft.UI.Xaml.appx
string appxFilePath = Path.Combine(extractedFolderPath, "tools", "AppX", "x64", "Release", "Microsoft.UI.Xaml.2.7.appx");
Console.WriteLine("Installing Microsoft.UI.Xaml.appx...");
InstallAppxPackage(appxFilePath);
// Step 4: Download and install the Winget MSIX bundle
string wingetPackageUrl = "https://github.com/microsoft/winget-cli/releases/latest/download/Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle";
string wingetMsixBundleFilePath = "./Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle";
Console.WriteLine($"Downloading {wingetPackageUrl}...");
DownloadFile(wingetPackageUrl, wingetMsixBundleFilePath);
Console.WriteLine("Installing Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle...");
InstallAppxPackage(wingetMsixBundleFilePath);
Console.WriteLine("Installation completed successfully.");
}
catch (Exception ex)
{
Console.WriteLine("Error occurred during installation:");
Console.WriteLine(ex);
//Join ex.Data key value pairs to string
if (ex.Data.Count > 0)
{
Console.WriteLine(" Extra details:");
foreach (DictionaryEntry de in ex.Data)
Console.WriteLine(" Key: {0,-20} Value: {1}",
"'" + de.Key + "'", de.Value);
}
//Join ex.Data key value pairs to string
if (ex.InnerException?.Data.Count > 0)
{
Console.WriteLine(" Extra details:");
foreach (DictionaryEntry de in ex.InnerException.Data)
Console.WriteLine(" Key: {0,-20} Value: {1}",
"'" + de.Key + "'", de.Value);
}
}
Console.ReadLine();
}
static void DownloadFile(string url, string filePath)
{
using (WebClient webClient = new WebClient())
{
webClient.DownloadFile(url, filePath);
}
}
static void ExtractZipFile(string zipFilePath, string extractPath)
{
ZipFile.ExtractToDirectory(zipFilePath, extractPath);
}
static void InstallAppxPackage(string packagePath)
{
PackageManager packageManager = new PackageManager();
var deploymentResult = packageManager.AddPackageAsync(new Uri(Path.GetFullPath(packagePath)), null, DeploymentOptions.None).AsTask().Result;
if (deploymentResult.IsRegistered)
{
Console.WriteLine("Package installed successfully.");
}
else
{
Console.WriteLine("Failed to install the package.");
}
}
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0-windows10.0.19041.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment