/// <summary>
/// 判断程序是否是以管理员身份运行。
/// </summary>
public static bool IsRunAsAdmin()
{
WindowsIdentity id = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(id);
return principal.IsInRole(WindowsBuiltInRole.Administrator);
}
//不是以管理员身份开启,则自动以管理员身份重新打开程序
//写在构造里比较省资源
public LoginFrm()
{
try
{
//判断是否以管理员身份运行,不是则提示
if (!PublicUtil.IsRunAsAdmin())
{
ProcessStartInfo psi = new ProcessStartInfo();
psi.WorkingDirectory = Environment.CurrentDirectory;
psi.FileName = Application.ExecutablePath;
psi.UseShellExecute = true;
psi.Verb = "runas";
Process p = new Process();
p.StartInfo = psi;
p.Start();
Process.GetCurrentProcess().Kill();
}
}
catch (Exception ex)
{
ExceptionScheduler.ExceptionScheduler exceptionScheduler = new ExceptionScheduler.ExceptionScheduler(ex);
ShowMessageOnUI.ShowErrorMessage("程序无法获取Windows管理员身份运行,\n请手动使用Windows管理员身份运行");
}
InitializeComponent();
}

C#判断程序是否以管理员身份运行,否则以管理员身份重新打开的更多相关文章

  1. 判断程序是否在VMWare内运行

    现在有许多用户都喜欢用虚拟机来测试他们的软件,以避免对真实机器环境造成损害.但是在虚拟机中,有些功能是受限,甚至不可能完成的,因此,需要在程序中判断虚拟机的环境,如果程序在虚拟机内运行,则就要把虚拟机 ...

  2. Win7下判断当前进程是否以管理员身份运行

    判断当前程序是否以管理员身份运行,代码如下: #include <iostream> #include <windows.h> using namespace std; // ...

  3. Runas命令:能让域用户/普通User用户以管理员身份运行指定程序。

    注:本文由Colin撰写,版权所有!转载请注明原文地址,谢谢合作! 在某些情况下,为了安全起见,大部分公司都会使用域控制器或只会给员工电脑user的用户权限,这样做能大大提高安全性和可控性,但由此也带 ...

  4. 【转】vc++MCF/C++/C中怎样让应用程序获得或以管理员权限运行 ,ShellExecuteEX编程 --- 获取管理员权限

    ShellExecuteEX编程 --- 获取管理员权限:http://blog.csdn.net/jhui163/article/details/5873027 怎样让你的应用程序获得管理员权限:就 ...

  5. 修改hosts文件(判断是否为管理员/以管理员权限运行脚本)

    将以下命令保存为 HostsModify.ps1,然后执行即可 #该脚本用来添加hosts解析记录.脚本在执行的时候会判断当前用户是否为管理员,如果不是则弹出提示框口,要求输入相应密码 If (-NO ...

  6. runas /user:administrator cmd 以管理员身份运行CMD

    runas /user:administrator cmd 以管理员身份运行CMD 1.windows+r打开 2.然后根据提示输入密码

  7. C# 之 判断或设置以管理员身份运行程序

    一.判断程序是否以管理员权限运行 using System.Security.Principal; public bool IsAdministrator() { WindowsIdentity cu ...

  8. C#以管理员身份运行程序

    using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; names ...

  9. 【CITE】C#默认以管理员身份运行程序实现代码

    //用于一种情况:C#软件打包后,在读写C盘文件时,会出现权限问题.使用管理员身份才可以运行 using System; using System.Collections.Generic; using ...

随机推荐

  1. Python列表和字典的方法总结

    列表方法: 方法 说明 append( item ) 在列表末尾插入(item ) count( element ) 返回element在列表中出现的次数 extend( newlist ) 将new ...

  2. jQuery.noConflict()防冲突机制

    许多JS库都非常喜欢使用$作为函数.变量.有时候,由于页面复杂的历史问题,或者为了实现特定的功能,我们不得不在页面中引入多个JS库.今儿个来学习学习jQuery库是怎么解决$冲突问题.(jQuery- ...

  3. C#开发学习——.net C#中页面之间传值传参的方法以及内置对象

    1.QueryString是一种非常简单的传值方式,他可以将传送的值显示在浏览器的地址栏中.如果是传递一个或多个安全性要求不高或是结构简单的数值时,可以使用这个方法.但是对于传递数组或对象的话,就不能 ...

  4. ChatCell

    import UIKit let chatCellHeight: CGFloat = 72 let chatCellInsetLeft =  chatCellHeight + 8 class Chat ...

  5. QWidget中嵌入win32 window(使用QWindow和QWidget::createWindowContainer)

    主要用到QWindow::fromWinId和QWidget::createWindowContainer这两个函数 QWindow::fromWinId用来创建一个win32窗口的代理 QWidge ...

  6. 无向图求割点 UVA 315 Network

    输入数据处理正确其余的就是套强联通的模板了 #include <iostream> #include <cstdlib> #include <cstdio> #in ...

  7. C++: Command Line Processing

    Save this code here for studying and using it. Surce code is here. CmdLine.h File #pragma once #incl ...

  8. Binary Search Tree Iterator——LeetCode

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  9. [log4j] 可用案例

    可用的 log4j 配置, log4j.properties log = /Users/grs/Documents/log4j log4j.rootLogger = DEBUG, FILEROLING ...

  10. eclipse 错误: 找不到或无法加载主类

    在src文件夹上移除Source Folder,再点右键-Build Path-Use as Source Folder,重新进行编译,一切正常了.