我们如何制作以管理员身份自动运行的c#应用程序 [英] how we can make c# application that automatically run in as administrator

查看:100
本文介绍了我们如何制作以管理员身份自动运行的c#应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 使用系统; 
使用 System.Collections.Generic;
使用 System.Text;
使用 Microsoft.Win32;

命名空间 msdn
{
class 计划
{
静态 void Main( string [] args)

{

const string NO_DRIVE = @ Software\Microsoft \ Windows \ CurrentVersion \POLicies\Explorer ;
尝试
{
使用(RegistryKey registryKey = Registry.CurrentUser .CreateSubKey(NO_DRIVE))
{
// 你的普通代码就在这里。
registryKey.SetValue( NoDrives
Convert.ToInt32(< span class =code-string>
8 16 ),RegistryValueKind。 DWORD);
registryKey.Close();

Console.WriteLine( 成功锁定);
};
}
catch (UnauthorizedAccessException E)
{
Console.WriteLine( 不允许访问。);
}

Console.ReadLine();
}
}
}

解决方案

您好,



您无法使用管理员权限自动运行应用程序。但您可以轻松提示提升请求用户允许应用程序以管理员权限运行。



这是一种方法:



首先使用Win32检查当前用户是否具有管理员资格:



 [DllImport(   shell32.dll)] 
internal 静态 extern bool IsUserAnAdmin ();





然后写一个方法来提升你的过程:

 私有 静态  bool 提升( string  processPath)
{
bool elevated = ;
if (!IsUserAnAdmin())
{
elevated = true ;
ProcessStartInfo procInfo = new ProcessStartInfo(processPath);
procInfo.UseShellExecute = true ;
procInfo.Verb = runas;
procInfo.WindowStyle = ProcessWindowStyle.Normal;

Process.Start(procInfo);
}
返回提升;
}





最后把它们放在一起:

 [DllImport(  shell32.dll)] 
内部 静态 extern bool IsUserAnAdmin();

public static void Main( string [] args)
{
if (! Elevate(System.Reflection.Assembly.GetExecutingAssembly()。Location))
{
const string NO_DRIVE = @ Software\Microsoft \ Windows \ CurrentVersion \POLicies\Explorer< /跨度>;
尝试
{
使用(RegistryKey registryKey = Registry.CurrentUser .CreateSubKey(NO_DRIVE))
{
// 你的普通代码就在这里。
registryKey.SetValue( NoDrives
Convert.ToInt32(< span class =code-string>
8 16 ),RegistryValueKind。 DWORD);
registryKey.Close();

Console.WriteLine( 成功锁定);
};
}
catch (UnauthorizedAccessException E)
{
Console.WriteLine( 不允许访问。);
}

Console.ReadLine();
}
}

私有 静态 bool 提升( string processPath)
{
bool elevated = false ;
if (!IsUserAnAdmin())
{
elevated = true ;
ProcessStartInfo procInfo = new ProcessStartInfo(processPath);
procInfo.UseShellExecute = true ;
procInfo.Verb = runas;
procInfo.WindowStyle = ProcessWindowStyle.Normal;

Process.Start(procInfo);
}
返回提升;
}





当第一次执行代码时,你会调用win32 api来告诉你用户是否是否拥有管理员权限,如果没有,您使用触发提升请求的runas动词启动您的流程,并且您的流程未以管理员身份运行。



是使用应用程序清单等实现相同的其他方式...谷歌是你的朋友...





Valery。


using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;

namespace msdn
{
    class Program
    {
        static void Main(string[] args)

        {

            const string NO_DRIVE = @"Software\Microsoft\Windows\CurrentVersion\Policies\Explorer";
            try
            {
                using (RegistryKey registryKey = Registry.CurrentUser.CreateSubKey(NO_DRIVE))
                {
                    // Your normal code goes here.
                    registryKey.SetValue("NoDrives",
                    Convert.ToInt32("8", 16), RegistryValueKind.DWord);
                    registryKey.Close();

                    Console.WriteLine("Successfully Locked");
                };
            }
            catch (UnauthorizedAccessException E)
            {
                Console.WriteLine("Access not allowed.");
            }

            Console.ReadLine();
        }
    }
}

解决方案

Hello,

You can't make your application run automatically with administrator privileges. But you can easily prompt for elevation requesting the user to allow the application to run with administrator rights.

Here is a way to do it:

First use Win32 to check if the current user has admin rigths:

[DllImport("shell32.dll")]
internal static extern bool IsUserAnAdmin();



Then write a method to elevate your process:

private static bool Elevate(string processPath)
{
    bool elevated = false;
    if (!IsUserAnAdmin())
    {
        elevated = true;
        ProcessStartInfo procInfo = new ProcessStartInfo(processPath);
        procInfo.UseShellExecute = true;
        procInfo.Verb = "runas";
        procInfo.WindowStyle = ProcessWindowStyle.Normal;

        Process.Start(procInfo);
    }
    return elevated;
}



Finally put it all together:

[DllImport("shell32.dll")]
internal static extern bool IsUserAnAdmin();

public static void Main(string[] args)
{
    if (!Elevate(System.Reflection.Assembly.GetExecutingAssembly().Location))
    {
        const string NO_DRIVE = @"Software\Microsoft\Windows\CurrentVersion\Policies\Explorer";
        try
        {
            using (RegistryKey registryKey = Registry.CurrentUser.CreateSubKey(NO_DRIVE))
            {
                // Your normal code goes here.
                registryKey.SetValue("NoDrives",
                Convert.ToInt32("8", 16), RegistryValueKind.DWord);
                registryKey.Close();

                Console.WriteLine("Successfully Locked");
            };
        }
        catch (UnauthorizedAccessException E)
        {
            Console.WriteLine("Access not allowed.");
        }

        Console.ReadLine();
    }
}

private static bool Elevate(string processPath)
{
    bool elevated = false;
    if (!IsUserAnAdmin())
    {
        elevated = true;
        ProcessStartInfo procInfo = new ProcessStartInfo(processPath);
        procInfo.UseShellExecute = true;
        procInfo.Verb = "runas";
        procInfo.WindowStyle = ProcessWindowStyle.Normal;

        Process.Start(procInfo);
    }
    return elevated;
}



What happens is that first time the code is executes, you call a win32 api that tells you if the user has admin rights or not, if not, you run startup your process using the "runas" verb that triggers elevation request and your process is not running as administrator.

There are other way to achieve the same using application manifest etc... Google is your friend...


Valery.


这篇关于我们如何制作以管理员身份自动运行的c#应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆