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

 public static void Load()
    {
        if (NeedAdmin())
        {
            new Form().ShowDialog();
            Environment.Exit(0);
        }
    }
    public static bool NeedAdmin()
    {
        bool result;
        if (Environment.OSVersion.Version.Major >= 6)
        {
            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 > 1)
        {
            string text = "";
            for (int i = 1; i < commandLineArgs.Length; i++)
            {
                text += commandLineArgs[i];
            }
            processStartInfo.Arguments = text;
        }
        try
        {
            Process.Start(processStartInfo);
            base.Close();
        }
        catch
        {
            base.Close();
        }
    }

 

posted on 2015-05-15 23:44  z5337  阅读(219)  评论(0)    收藏  举报