mirror of
https://github.com/wwiinnddyy/LanMountainDesktop.git
synced 2026-06-22 17:24:27 +08:00
feat.PLONDS在线安装器继续优化
This commit is contained in:
52
LanDesktopPLONDS.installer/Services/InstallerElevation.cs
Normal file
52
LanDesktopPLONDS.installer/Services/InstallerElevation.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using System.Security.Principal;
|
||||
|
||||
namespace LanDesktopPLONDS.Installer.Services;
|
||||
|
||||
internal static class InstallerElevation
|
||||
{
|
||||
public static bool IsRunningElevated()
|
||||
{
|
||||
if (!OperatingSystem.IsWindows())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
using var identity = WindowsIdentity.GetCurrent();
|
||||
var principal = new WindowsPrincipal(identity);
|
||||
return principal.IsInRole(WindowsBuiltInRole.Administrator);
|
||||
}
|
||||
|
||||
public static bool RequiresElevation(string installPath)
|
||||
{
|
||||
if (!OperatingSystem.IsWindows())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var fullPath = Path.GetFullPath(installPath);
|
||||
return IsUnderSpecialFolder(fullPath, Environment.SpecialFolder.ProgramFiles)
|
||||
|| IsUnderSpecialFolder(fullPath, Environment.SpecialFolder.ProgramFilesX86)
|
||||
|| IsUnderWindowsDirectory(fullPath);
|
||||
}
|
||||
|
||||
public static void EnsureCanInstall(string installPath)
|
||||
{
|
||||
if (RequiresElevation(installPath) && !IsRunningElevated())
|
||||
{
|
||||
throw new UnauthorizedAccessException(
|
||||
"The selected installation path requires administrator permission. Restart the installer as administrator or choose a user-writable folder.");
|
||||
}
|
||||
}
|
||||
|
||||
private static bool IsUnderSpecialFolder(string fullPath, Environment.SpecialFolder folder)
|
||||
{
|
||||
var root = Environment.GetFolderPath(folder);
|
||||
return !string.IsNullOrWhiteSpace(root) && InstallerPathGuard.IsSameOrChildPath(root, fullPath);
|
||||
}
|
||||
|
||||
private static bool IsUnderWindowsDirectory(string fullPath)
|
||||
{
|
||||
var windows = Environment.GetFolderPath(Environment.SpecialFolder.Windows);
|
||||
return !string.IsNullOrWhiteSpace(windows) && InstallerPathGuard.IsSameOrChildPath(windows, fullPath);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user