How do I give my windows service admin rights

好久不见. 提交于 2019-12-10 19:46:27

问题


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();
    }

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.


回答1:


Use user and password for the service, and assure that the user that start the service has all rights needed for the application to run. An other possibility if you can control the service is to use impersonation, see: http://www.codeproject.com/Articles/4051/Windows-Impersonation-using-C.




回答2:


You can try couple of options.

  1. Add app.manifest
  2. After installing window service set the service permission to run at Highest Privilege and also you can set the user name and password for the logged in user.



回答3:


How to Get Full Administrator Rights in Windows 7:

  1. Click Start
  2. Click Computer (you might also find this icon on the desktop).
  3. Right click on the Hard Disk icon where your OS is installed on and click Properties.
  4. Click the Security tab.
  5. Click the Advanced tab.
  6. Click the Change Permissions button located after the Permission Entries list.
  7. A new window will appear on your screen; which contains a list of all the User Accounts
  8. Select the user account you want to give total control over your Windows 7) and click the Edit button.
  9. Now, tick the checkbox labeled "Total Control" and press OK.

You're all done!



来源:https://stackoverflow.com/questions/13082284/how-do-i-give-my-windows-service-admin-rights

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!