Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ConsoleApp/ConsoleApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<ErrorReport>none</ErrorReport>
<AnalysisLevel>latest-recommended</AnalysisLevel>
<StartupObject>ConsoleApp.Program</StartupObject>
<Version>0.0.7.0</Version>
<Version>0.0.8.0</Version>
<BuiltInComInteropSupport>true</BuiltInComInteropSupport>
<ApplicationManifest>app.manifest</ApplicationManifest>
</PropertyGroup>
Expand Down
4 changes: 2 additions & 2 deletions ConsoleApp/src/ArgumentParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ internal static void ParseArgs(ref Parameters parameters, string[] args)
parameters.ImageFilePath = args[Array.IndexOf(args, arg) + 1].ToLowerInvariant();
continue;
case "/additionaldriversdrive":
parameters.AdditionalDriversDrive = args[Array.IndexOf(args, arg) + 1].ToLowerInvariant();
parameters.AdditionalDrive = args[Array.IndexOf(args, arg) + 1].ToLowerInvariant();
continue;
case "/firmwaretype":
parameters.FirmwareType = args[Array.IndexOf(args, arg) + 1].ToUpperInvariant();
Expand All @@ -80,7 +80,7 @@ internal static void ParseArgs(ref Parameters parameters, string[] args)
Console.WriteLine($" Source Drive: {parameters.SourceDrive}");
Console.WriteLine($" Image Index: {parameters.ImageIndex}");
Console.WriteLine($" Image File Path: {parameters.ImageFilePath}");
Console.WriteLine($" Additional Drivers Drive: {parameters.AdditionalDriversDrive}");
Console.WriteLine($" Additional Drivers Drive: {parameters.AdditionalDrive}");
Console.WriteLine($" Firmware Type: {parameters.FirmwareType}");
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
Expand Down
25 changes: 17 additions & 8 deletions ConsoleApp/src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,36 +30,45 @@ internal static int Main(string[] args)
Console.WriteLine($"Created by {ProgramInfo.GetAuthor()}");
#endif
}
catch (Exception)
catch (Exception ex)
{
throw;
Console.WriteLine($"An error has occurred: {ex.Message}");
}

try
{
ArgumentParser.ParseArgs(ref parameters, args);
}
catch (Exception)
catch (Exception ex)
{
throw;
Console.WriteLine($"An error has occurred: {ex.Message}");
}

try
{
InstallerManager.Configure(ref parameters);
}
catch (Exception)
catch (Exception ex)
{
throw;
Console.WriteLine($"An error has occurred: {ex.Message}");
}

try
{
InstallerManager.Prepare(ref parameters);
}
catch (Exception ex)
{
Console.WriteLine($"An error has occurred: {ex.Message}");
}

try
{
InstallerManager.InstallWindows(ref parameters);
}
catch (Exception)
catch (Exception ex)
{
throw;
Console.WriteLine($"An error has occurred: {ex.Message}");
}

return 0;
Expand Down
2 changes: 1 addition & 1 deletion WindowsInstallerLib/WindowsInstallerLib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<ErrorReport>none</ErrorReport>
<Title>$(AssemblyName)</Title>
<LangVersion>latest</LangVersion>
<Version>1.1.4.0</Version>
<Version>1.1.5.0</Version>
<Platforms>x64</Platforms>
<EnableWindowsTargeting>true</EnableWindowsTargeting>
</PropertyGroup>
Expand Down
72 changes: 29 additions & 43 deletions WindowsInstallerLib/src/DeployManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -337,49 +337,35 @@ internal static DismImageInfoCollection GetImageInfoT(ref Parameters parameters)
/// <exception cref="UnauthorizedAccessException"></exception>
internal static void InstallAdditionalDrivers(ref Parameters parameters)
{
bool IS_ADMIN = PrivilegesManager.IsAdmin();

DismSession? session = null;

ArgumentException.ThrowIfNullOrWhiteSpace(parameters.AdditionalDriversDrive, nameof(parameters));
ArgumentException.ThrowIfNullOrWhiteSpace(parameters.AdditionalDrive, nameof(parameters));

if (!PrivilegesManager.IsAdmin())
switch (IS_ADMIN)
{
throw new UnauthorizedAccessException("You do not have enough privileges to install additional drivers.");
case true:
try
{
DismApi.Initialize(DismLogLevel.LogErrorsWarnings);
}
catch (DismException)
{
throw;
}
catch (Exception)
{
throw;
}
break;
case false:
throw new UnauthorizedAccessException("You do not have enough privileges to initialize the DISM API.");
}

try
{
switch (PrivilegesManager.IsAdmin())
{
case true:
try
{
DismApi.Initialize(DismLogLevel.LogErrorsWarnings);
}
catch (DismException)
{
throw;
}
catch (Exception)
{
throw;
}
break;
case false:
throw new UnauthorizedAccessException("You do not have enough privileges to initialize the DISM API.");
}

try
{
session ??= DismApi.OpenOfflineSession(parameters.DestinationDrive);
}
catch (DismException)
{
throw;
}
catch (Exception)
{
throw;
}
session ??= DismApi.OpenOfflineSession(parameters.DestinationDrive);
}
catch (DismException)
{
Expand All @@ -392,7 +378,7 @@ internal static void InstallAdditionalDrivers(ref Parameters parameters)

try
{
DismApi.AddDriversEx(session, parameters.AdditionalDriversDrive, false, true);
DismApi.AddDriversEx(session, parameters.AdditionalDrive, false, true);
}
finally
{
Expand All @@ -416,9 +402,11 @@ internal static void InstallAdditionalDrivers(ref Parameters parameters)
/// <exception cref="UnauthorizedAccessException">Thrown if the current process does not have administrative privileges required to perform the installation.</exception>
internal static int InstallBootloader(ref Parameters parameters)
{
ArgumentException.ThrowIfNullOrWhiteSpace(parameters.DestinationDrive, nameof(parameters.DestinationDrive));
ArgumentException.ThrowIfNullOrWhiteSpace(parameters.EfiDrive, nameof(parameters.EfiDrive));
ArgumentException.ThrowIfNullOrWhiteSpace(parameters.FirmwareType, nameof(parameters.FirmwareType));
ArgumentException.ThrowIfNullOrWhiteSpace(parameters.DestinationDrive, nameof(parameters));
ArgumentException.ThrowIfNullOrWhiteSpace(parameters.EfiDrive, nameof(parameters));
ArgumentException.ThrowIfNullOrWhiteSpace(parameters.FirmwareType, nameof(parameters));

bool IS_ADMIN = PrivilegesManager.IsAdmin();

string EFI_BOOT_PATH = Path.Join(parameters.EfiDrive, @"\EFI\Boot");
string EFI_MICROSOFT_PATH = Path.Join(parameters.EfiDrive, @"\EFI\Microsoft");
Expand All @@ -428,7 +416,7 @@ internal static int InstallBootloader(ref Parameters parameters)
bool EFI_MICROSOFT_EXISTS = Directory.Exists(EFI_MICROSOFT_PATH);
bool WINDIR_EXISTS = Directory.Exists(WINDIR_PATH);

if (!PrivilegesManager.IsAdmin())
if (IS_ADMIN)
{
throw new UnauthorizedAccessException($"You do not have enough privileges to install the bootloader to {parameters.EfiDrive}");
}
Expand All @@ -445,9 +433,8 @@ internal static int InstallBootloader(ref Parameters parameters)
throw new DirectoryNotFoundException(@$"The directory {WINDIR_PATH} does not exist!");
}

Console.WriteLine($"Firmware type is set to: {parameters.FirmwareType}");
Console.WriteLine($"\n==> Installing bootloader to drive {parameters.EfiDrive} in disk {parameters.DiskNumber}");
ProcessManager.StartCmdProcess("bcdboot", @$"{WINDIR_PATH} /s {parameters.EfiDrive} /f {parameters.FirmwareType}");
ProcessManager.StartCmdProcess("bcdboot.exe", @$"{WINDIR_PATH} /s {parameters.EfiDrive} /f {parameters.FirmwareType}");
}
catch (IOException)
{
Expand All @@ -457,7 +444,6 @@ internal static int InstallBootloader(ref Parameters parameters)
{
throw;
}

return ProcessManager.ExitCode;
}
}
Expand Down
50 changes: 17 additions & 33 deletions WindowsInstallerLib/src/DiskManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Management;
Expand Down Expand Up @@ -51,50 +53,32 @@ internal static int FormatDisk(ref Parameters parameters)
/// model, and device ID for each disk drive found. <para> Note that this method is intended for internal use
/// and writes directly to the console. It does not return the retrieved data or provide a way to
/// programmatically access it. </para></remarks>
internal static void ListAll()
internal static SortedDictionary<int, string> GetDisks()
{
SortedDictionary<int, string> SystemDisks = [];

try
{
WqlObjectQuery DeviceTable = new("SELECT * FROM Win32_DiskDrive");
ManagementObjectSearcher DeviceInfo = new(DeviceTable);
foreach (ManagementObject o in DeviceInfo.Get().Cast<ManagementObject>())
WqlObjectQuery Query = new("SELECT * FROM Win32_DiskDrive");
ManagementObjectSearcher ObjectSearcher = new(Query);
foreach (ManagementObject obj in ObjectSearcher.Get().Cast<ManagementObject>())
{
Console.WriteLine("Disk number = " + o["Index"]);
Console.WriteLine("Model = " + o["Model"]);
Console.WriteLine("DeviceID = " + o["DeviceID"]);
Console.WriteLine("");
}
}
catch (Exception)
{
throw;
}
}
int Index = Convert.ToInt32(obj["Index"], CultureInfo.CurrentCulture);
string? Model = obj["Model"].ToString();

/// <summary>
/// Retrieves an array of all available drive information on the system.
/// </summary>
/// <returns>An array of <see cref="DriveInfo"/> objects representing the drives available on the system.</returns>
internal static DriveInfo[] GetDisksT()
{
try
{
DriveInfo[] drives = DriveInfo.GetDrives();
if (Model != null)
{
SystemDisks.Add(Index, Model);
}
}

return drives;
}
catch (IOException)
{
throw;
}
catch (UnauthorizedAccessException)
{
throw;
}
catch (Exception)
{
throw;
}

return SystemDisks;
}
}
}
Loading
Loading