[转]判断是否 Win7 且需要管理员权限

时间:2022-05-12 18:15:23
 public static void Load()
{
if (NeedAdmin())
{
new Form().ShowDialog();
Environment.Exit();
}
}
public static bool NeedAdmin()
{
bool result;
if (Environment.OSVersion.Version.Major >= )
{
WindowsIdentity current = WindowsIdentity.GetCurrent();
WindowsPrincipal windowsPrincipal = new WindowsPrincipal(current);
result = !windowsPrincipal.IsInRole(WindowsBuiltInRole.Administrator);
}
else
{
result = false;
}
return result;
}
private void BtnRunasAdmin_Click(object sender, EventArgs e)
{
ProcessStartInfo processStartInfo = new ProcessStartInfo();
processStartInfo.WorkingDirectory = Application.StartupPath;
processStartInfo.FileName = Path.GetFileName(Application.ExecutablePath);
processStartInfo.Verb = "runas";
string[] commandLineArgs = Environment.GetCommandLineArgs();
if (commandLineArgs.Length > )
{
string text = "";
for (int i = ; i < commandLineArgs.Length; i++)
{
text += commandLineArgs[i];
}
processStartInfo.Arguments = text;
}
try
{
Process.Start(processStartInfo);
base.Close();
}
catch
{
base.Close();
}
}