Skip to content

Commit 7c88205

Browse files
committed
Created the static SystemHelper class and moved fiitting methods into it, for improved structure
1 parent 10e9c5e commit 7c88205

3 files changed

Lines changed: 87 additions & 67 deletions

File tree

src/WGet.NET/Components/WinGet.cs

Lines changed: 2 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System.Collections.Generic;
77
using System.IO;
88
using System.Security;
9-
using System.Security.Principal;
109
using System.Threading;
1110
using System.Threading.Tasks;
1211
using WGetNET.Builder;
@@ -131,7 +130,7 @@ public Version Version
131130
public WinGet()
132131
{
133132
// Check if the current process has administrator privileges
134-
_administratorPrivileges = CheckAdministratorPrivileges();
133+
_administratorPrivileges = SystemHelper.CheckAdministratorPrivileges();
135134

136135
// Set inital values
137136
_processManager = new ProcessManager("winget");
@@ -860,35 +859,6 @@ private string CheckWinGetVersion()
860859
return string.Empty;
861860
}
862861

863-
/// <summary>
864-
/// Checks if winget is installed on the system and returns the path to the executable.
865-
/// </summary>
866-
/// <returns>
867-
/// <see cref="System.String"/> containing the executable path if it was found or <see cref="System.String.Empty"/> if not.
868-
/// </returns>
869-
private string CheckInstallation()
870-
{
871-
string? pathEnvVar = Environment.GetEnvironmentVariable("Path", EnvironmentVariableTarget.User);
872-
if (string.IsNullOrWhiteSpace(pathEnvVar))
873-
{
874-
return string.Empty;
875-
}
876-
877-
string[] paths = pathEnvVar.Split(';');
878-
879-
string exePath;
880-
for (int i = 0; i < paths.Length; i++)
881-
{
882-
exePath = Path.Combine(paths[i], "winget.exe");
883-
if (File.Exists(exePath))
884-
{
885-
return exePath;
886-
}
887-
}
888-
889-
return string.Empty;
890-
}
891-
892862
/// <summary>
893863
/// Gets the last modification date (UTC) of the currently set winget executable.
894864
/// </summary>
@@ -915,7 +885,7 @@ private bool QueryInstallation()
915885
{
916886
bool isInstalled;
917887

918-
_wingetExePath = CheckInstallation();
888+
_wingetExePath = SystemHelper.CheckWingetInstallation();
919889

920890
if (string.IsNullOrWhiteSpace(_wingetExePath))
921891
{
@@ -945,32 +915,6 @@ private bool QueryInstallation()
945915

946916
return isInstalled;
947917
}
948-
949-
/// <summary>
950-
/// Check if the current user has administrator privileges.
951-
/// </summary>
952-
/// <returns>
953-
/// <see langword="true"/> if the current user has administrator privileges and
954-
/// <see langword="false"/> if not.
955-
/// </returns>
956-
private bool CheckAdministratorPrivileges()
957-
{
958-
if (Environment.OSVersion.Platform == PlatformID.Unix)
959-
{
960-
// Making sure windows related functions dont get called on none windows systems.
961-
return true;
962-
}
963-
964-
using WindowsIdentity? identity = WindowsIdentity.GetCurrent(false);
965-
966-
if (identity != null)
967-
{
968-
return new WindowsPrincipal(identity)
969-
.IsInRole(WindowsBuiltInRole.Administrator);
970-
}
971-
972-
return false;
973-
}
974918
//---------------------------------------------------------------------------------------------
975919
}
976920
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
//--------------------------------------------------//
2+
// Created by basicx-StrgV //
3+
// https://github.com/basicx-StrgV/ //
4+
//--------------------------------------------------//
5+
using System;
6+
using System.IO;
7+
using System.Security.Principal;
8+
9+
namespace WGetNET.Helper
10+
{
11+
/// <summary>
12+
/// The <see langword="static"/> <see cref="WGetNET.Helper.SystemHelper"/> class provides methods for system related tasks.
13+
/// </summary>
14+
internal static class SystemHelper
15+
{
16+
/// <summary>
17+
/// Check if the current user has administrator privileges.
18+
/// </summary>
19+
/// <returns>
20+
/// <see langword="true"/> if the current user has administrator privileges and
21+
/// <see langword="false"/> if not.
22+
/// </returns>
23+
public static bool CheckAdministratorPrivileges()
24+
{
25+
if (Environment.OSVersion.Platform == PlatformID.Unix)
26+
{
27+
// Making sure windows related functions dont get called on none windows systems.
28+
return false;
29+
}
30+
31+
using WindowsIdentity? identity = WindowsIdentity.GetCurrent(false);
32+
33+
if (identity != null)
34+
{
35+
return new WindowsPrincipal(identity)
36+
.IsInRole(WindowsBuiltInRole.Administrator);
37+
}
38+
39+
return false;
40+
}
41+
42+
/// <summary>
43+
/// Checks if winget is installed on the system and returns the path to the executable.
44+
/// </summary>
45+
/// <returns>
46+
/// <see cref="System.String"/> containing the executable path if it was found or <see cref="System.String.Empty"/> if not.
47+
/// </returns>
48+
public static string CheckWingetInstallation()
49+
{
50+
string? pathEnvVar = Environment.GetEnvironmentVariable("Path", EnvironmentVariableTarget.User);
51+
if (string.IsNullOrWhiteSpace(pathEnvVar))
52+
{
53+
return string.Empty;
54+
}
55+
56+
string[] paths = pathEnvVar.Split(';');
57+
58+
string exePath;
59+
for (int i = 0; i < paths.Length; i++)
60+
{
61+
exePath = Path.Combine(paths[i], "winget.exe");
62+
if (File.Exists(exePath))
63+
{
64+
return exePath;
65+
}
66+
}
67+
68+
return string.Empty;
69+
}
70+
}
71+
}

src/WGet.NET/XmlDocumentation/WGet.NET.xml

Lines changed: 14 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)