如何授予我的 Windows 服务管理员权限 [英] How do I give my windows service admin rights
问题描述
我有一个 winform,它允许我使用以下代码启用和禁用我 8 岁孩子的所有网络适配器:
I have a winform that allows me to enable and disable all my 8 year old's network adapters using this code:
protected override void OnStart(string[] args)
{
//start timer
SelectQuery query = new SelectQuery("Win32_NetworkAdapter","NetConnectionStatus=2");
ManagementObjectSearcher search = new ManagementObjectSearcher(query);
foreach (ManagementObject result in search.Get())
{
NetworkAdapter adapter = new NetworkAdapter(result);
adapter.Disable();
enabled = false;
}
InternetCheckTimer.Start();
}
假设我使用管理员运行,此代码在 win 表单上运行良好.我之前从未编写过 win 服务,所以问题可能出在其他地方,我可以使用 installtill 进行安装并将调试器附加到进程中,但是没有遇到断点.我试过启动和停止进程,但无法连接调试器,所以我也可能做错了……现在我假设代码正在运行,但我太愚蠢了,无法让调试器工作.也就是说,我认为我的代码需要该服务具有像表单那样的管理员才能工作.
This code works fine on a win form assuming I am running with admin. I have never written a win service before so the problem might be else where, I am able to install using installutill and attach the debugger to the process, however no break points are hit. I have tried starting and stopping the process and cannot get the debugger to attach so I might be doing that wrong as well... Right now I am assuming that the code is running and I am too stupid to get the debugger working. That said, I think my code requires the service to have admin like the form did in order to work.
抱歉,如果不清楚,如果您需要更多信息,我会尽力澄清.
Sorry if this is unclear, I will do my best to clear it up if you need more information.
推荐答案
您可以尝试几个选项.
- 添加 app.manifest
- 安装窗口服务后,设置服务权限以最高权限运行,您还可以为登录用户设置用户名和密码.
这篇关于如何授予我的 Windows 服务管理员权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!