UAC或Userlevel如何克服呢!
我正在尝试以编程方式从用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
像这样创建一个新的控制台应用程序项目:
在您的项目中,添加对上层项目的引用并使用类似的内容来调用可执行文件。不使用 runas 动词,它会提示用户提升权限。
Make a new console app project like this:
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.
您不能跳过/忽略 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.