UAC或Userlevel如何克服呢!

发布于 2024-10-04 07:05:28 字数 1062 浏览 6 评论 0原文

我正在尝试以编程方式从用 C#(在 .net 4.0 上)编写的帮助程序应用程序中重新启动服务,但如果我在右键单击并执行“以管理员身份运行”的情况下双击来运行 EXE,则会出现权限冲突。

但为什么我需要这个用户是本地管理员?!

我希望应用程序正常运行,并且仅在用户单击按钮重新启动服务时请求管理员权限。这可以做到吗?

该解决方案需要在 xp、vista 和 windows 7 上运行。

我正在使用 中的代码http://www.csharp-examples.net/restart-windows-service/

public static void RestartService(string serviceName, int timeoutMilliseconds)
{
  ServiceController service = new ServiceController(serviceName);
  try
  {
    int millisec1 = Environment.TickCount;
    TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);

    service.Stop();
    service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);

    // count the rest of the timeout
    int millisec2 = Environment.TickCount;
    timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds - (millisec2-millisec1));

    service.Start();
    service.WaitForStatus(ServiceControllerStatus.Running, timeout);
  }
  catch
  {
    // ...
  }
}

I'm trying to programmatically restart a service from within a helper application written in C# (on .net 4.0) but I get a permission violation if I run the EXE by double clicking while right clicking and doing "run as Administrator" works.

But why do I need this the users are local admins?!

I would like the app to run normally and only request admin rights when the user click the button to restart the service. Can this be done?

The solution needs to work on xp, vista and windows 7.

I'm using this code from http://www.csharp-examples.net/restart-windows-service/

public static void RestartService(string serviceName, int timeoutMilliseconds)
{
  ServiceController service = new ServiceController(serviceName);
  try
  {
    int millisec1 = Environment.TickCount;
    TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);

    service.Stop();
    service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);

    // count the rest of the timeout
    int millisec2 = Environment.TickCount;
    timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds - (millisec2-millisec1));

    service.Start();
    service.WaitForStatus(ServiceControllerStatus.Running, timeout);
  }
  catch
  {
    // ...
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

┈┾☆殇 2024-10-11 07:05:28

像这样创建一个新的控制台应用程序项目:

public class RunService
{
 static int Main()(string[] args)
 {
  //TODO read serviceName and timeoutMilliseconds from args
  ServiceController service = new ServiceController(serviceName);
  try
  {
    int millisec1 = Environment.TickCount;
    TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);

    service.Stop();
    service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);

    // count the rest of the timeout
    int millisec2 = Environment.TickCount;
    timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds - (millisec2-millisec1));   service.Start();
service.WaitForStatus(ServiceControllerStatus.Running, timeout);
    // TODO return status code
  }
  catch
  { 
   // ...
  }
 }
}

在您的项目中,添加对上层项目的引用并使用类似的内容来调用可执行文件。不使用 runas 动词,它会提示用户提升权限。

var process = Process.Start(new ProcessStartInfo
                        {
                            Verb = "runas",
                            FileName = typeof(RunService).Assembly.Location,
                            UseShellExecute = true,
                            CreateNoWindow = true,
                        });
process.WaitForExit();
var exitCode = process.ExitCode
// TODO process exit code...

Make a new console app project like this:

public class RunService
{
 static int Main()(string[] args)
 {
  //TODO read serviceName and timeoutMilliseconds from args
  ServiceController service = new ServiceController(serviceName);
  try
  {
    int millisec1 = Environment.TickCount;
    TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);

    service.Stop();
    service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);

    // count the rest of the timeout
    int millisec2 = Environment.TickCount;
    timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds - (millisec2-millisec1));   service.Start();
service.WaitForStatus(ServiceControllerStatus.Running, timeout);
    // TODO return status code
  }
  catch
  { 
   // ...
  }
 }
}

In your project, add a reference to the upper project and use something like this to call the executable. Not the use of the runas verb, which will prompt the user for elevation rights.

var process = Process.Start(new ProcessStartInfo
                        {
                            Verb = "runas",
                            FileName = typeof(RunService).Assembly.Location,
                            UseShellExecute = true,
                            CreateNoWindow = true,
                        });
process.WaitForExit();
var exitCode = process.ExitCode
// TODO process exit code...
旧伤还要旧人安 2024-10-11 07:05:28

您不能跳过/忽略 UAC 权限请求(这实际上会否定它的整个使用),但是PrincipalPermissionAttribute 可能就是您要找的。虽然到目前为止还没有使用过它。

You can't skip/ignore the UAC permission request (that would essentially negate it's whole use) but PrincipalPermissionAttribute might be what you're looking for. Never used it so far though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文