首页 > 解决方案 > 在没有管理员权限的情况下同步时间 wint NTP 服务器

问题描述

我写了一个应用程序,它从 NTP 服务器获取时间并更改我机器的系统时间。它工作得很好,但只有当我的应用程序以管理员权限启动时才会同步时间。所以一个问题是如何在没有管理员权限的情况下启动它,但要保存它的功能?我使用 WinAPI 函数设置系统时间SetSystemTime

标签: c#ntpsystemtime

解决方案


您可以通过多种方式做到这一点(选择最适合您的一种)。

  1. 身份模拟 =>在此地址以示例进行描述

    WindowsIdentity identity = new WindowsIdentity(accessToken); WindowsImpersonationContext context = identity.Impersonate() ;

  2. runas 动词

    ProcessStartInfo startInfo = new ProcessStartInfo (m_strInstallUtil, strExePath); startInfo.Verb = "runas"; System.Diagnostics.Process.Start (startInfo);

请记住,将提示 UAC 使用第二种方法

  1. PrincipalPermissin(如果用户是 Admin 组的成员)

    [PrincipalPermission(SecurityAction.Demand, Role = @"BUILTIN\Administrators")]


推荐阅读