返回> 网站首页 

[转载]以管理员身份运行

yoours2015-06-09 14:12:57 阅读 1601

简介一边听听音乐,一边写写文章。

方法一: 
 
static void Main(string[] Args) 

    /** 
     * 当前用户是管理员的时候,直接启动应用程序 
     * 如果不是管理员,则使用启动对象启动程序,以确保使用管理员身份运行 
     */ 
    //获得当前登录的Windows用户标示 
    System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent(); 
    //创建Windows用户主题 
    Application.EnableVisualStyles(); 
 
    System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity); 
    //判断当前登录用户是否为管理员 
    if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator)) 
    { 
        //如果是管理员,则直接运行 
 
        Application.EnableVisualStyles(); 
        Application.Run(new Form1()); 
    } 
    else 
    { 
        //创建启动对象 
        System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(); 
        //设置运行文件 
        startInfo.FileName = System.Windows.Forms.Application.ExecutablePath; 
        //设置启动参数 
        //startInfo.Arguments = String.Join(" ", Args); 
        //设置启动动作,确保以管理员身份运行 
        startInfo.Verb = "runas"
        try 
        { 
            //如果不是管理员,则启动UAC 
            System.Diagnostics.Process.Start(startInfo); 
            //退出 
            System.Windows.Forms.Application.Exit(); 
        } 
        catch 
    { 
    } 


 
方法二 
VS2010  项目右键属性--安全性-- -启用ClickOnce安全设置 
 
然后程序目录下会生成一个app.manifest文件 
 
<requestedExecutionLevel level = "asInvoker" uiAccess = "false" /> 
    改为 
<requestedExecutionLevel level = "requireAdministrator" uiAccess = "false" /> 
然后在"安全性"中再勾去"启用ClickOnce安全设置"后,重新编译即可。 
 
 
1) 打开Vs2005或vs2008工程,看在Properties下是否有app.manifest这个文件; 如没有,右击工程在菜单中选择“属性”,出现界面如下: 
 选中"Security",在界面中勾选"Enable ClickOnce Security Settings"后,在Properties下就有自动生成app.manifest文件。 
  打开app.manifest文件,将 
<requestedExecutionLevel level = "asInvoker" uiAccess = "false" /> 
    改为 
<requestedExecutionLevel level = "requireAdministrator" uiAccess = "false" /> 
然后在"Security"中再勾去"Enable ClickOnce Security Settings"后,重新编译即可。 
微信小程序扫码登陆

文章评论

1601人参与,0条评论