Implementing IsAdministrator for non-Windows

This commit is contained in:
2dust
2024-10-06 10:23:44 +08:00
parent 75d86cf883
commit 409fe5290e
5 changed files with 42 additions and 45 deletions
+35 -18
View File
@@ -7,6 +7,7 @@ using System.Net.Sockets;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Security.Principal;
using System.Text;
using System.Text.RegularExpressions;
@@ -701,24 +702,6 @@ namespace ServiceLib.Common
return systemHosts;
}
public static string GetExeName(string name)
{
if (IsWindows())
{
return $"{name}.exe";
}
else
{
return name;
}
}
public static bool IsWindows() => RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
public static bool IsLinux() => RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
public static bool IsOSX() => RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
#endregion 杂项
#region TempPath
@@ -864,5 +847,39 @@ namespace ServiceLib.Common
}
#endregion TempPath
#region Platform
public static bool IsWindows() => RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
public static bool IsLinux() => RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
public static bool IsOSX() => RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
public static string GetExeName(string name)
{
if (IsWindows())
{
return $"{name}.exe";
}
else
{
return name;
}
}
public static bool IsAdministrator()
{
if (IsWindows())
{
return new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator);
}
else
{
return Mono.Unix.Native.Syscall.geteuid() == 0;
}
}
#endregion Platform
}
}