【问题标题】:start / stop windows service via C# with run as admin without UAC prompt window通过 C# 启动/停止 Windows 服务,以管理员身份运行,没有 UAC 提示窗口
【发布时间】:2014-01-09 05:49:04
【问题描述】:

我需要在每天的特定时间从 C# 代码启动/停止 Windows 服务。所以我写了一个简单的C#程序。我的程序仅在我以管理员身份运行时才有效。很好,我已经编写了代码以管理员身份运行我的程序。

现在我陷入了这样一种情况,即我的 C# 代码以“Admin”身份运行 exe 文件,但 UAC 窗口出现 program wants to change settings etc msg。

我需要我的 c# 代码文件在 Windows 调度程序下运行,这意味着不需要人工交互。对于 UAC 窗口,用户需要选择是。

我怎样才能摆脱它或解决这个问题,以便我的程序将在没有任何人工交互的情况下完全务实地执行和停止服务?

【问题讨论】:

    标签: c# uac


    【解决方案1】:

    创建一个接受 a 的程序版本

    Sam.exe /StopAndStopTheWindowsServiceINeedToSmack
    

    在您的应用启动时,检查命令行开关。如果存在,则停止/开始。

    然后创建一个使用命令行选项运行您的应用程序的计划任务,并设置以最高权限运行选项:

    然后使用Task Scheduler 2.0 API 以编程方式启动计划任务。

    Bonus Chatter:一段代码

    public Form1()
    {
        InitializeComponent();
    
        //Ideally this would be in program.cs, before the call to Application.Run()
        //But that would require me to refactor code out of the Form file, which is overkill for a demo
        if (FindCmdLineSwitch("StopAndStopTheWindowsServiceINeedToSmack", true))
        {
            RestartService("bthserv"); //"Bluetooth Support Service"
            Environment.Exit(0);
        }
    }
    
    private bool FindCmdLineSwitch(string Switch, bool IgnoreCase)
    {
        foreach (String s  in System.Environment.GetCommandLineArgs())
        {
            if (String.Compare(s, "/" + Switch, IgnoreCase) == 0)
                return true;
            if (String.Compare(s, "-" + Switch, IgnoreCase) == 0)
                return true;
        }
        return false;
    }
    

    然后我们重启服务:

    private void RestartService(String ServiceName)
    {
        TimeSpan timeout = TimeSpan.FromMilliseconds(30000); //30 seconds
    
        using (ServiceController service = new ServiceController(ServiceName))
        {
            try
            {
                service.Start();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "Error stopping service");
                return;
            }
            service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
    
            try
            {
                service.Start();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "Error starting service");
                return;
            }
            service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
        }
    }
    

    奖励:检查你是否在提升:

    private Boolean IsUserAnAdmin()
    {
        //A user can be a member of the Administrator group, but not an administrator.
        //Conversely, the user can be an administrator and not a member of the administrators group.
    
        //Check if the current user has administrative privelages
        var identity = WindowsIdentity.GetCurrent();
        return (null != identity && new WindowsPrincipal(identity).IsInRole(WindowsBuiltInRole.Administrator));
    }
    

    注意:任何发布到公共领域的代码。无需署名。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-06
      • 2018-03-13
      • 2015-09-26
      • 2014-09-10
      • 2019-07-30
      • 1970-01-01
      • 2011-01-15
      • 1970-01-01
      相关资源
      最近更新 更多