3

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.

Contristo
  • 315
  • 1
  • 3
  • 17
  • Possible duplicate of [How do I install a windows service that runs as an administrator?](https://stackoverflow.com/questions/3755051/how-do-i-install-a-windows-service-that-runs-as-an-administrator) – Hoppeduppeanut Sep 25 '18 at 04:05

3 Answers3

0

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.

ctescu
  • 350
  • 4
  • 16
  • I tried user, when the service started it asked for a username and password. It rollbacked the install everytime and I am the admin :-/ – Contristo Oct 26 '12 at 07:22
  • The user used has SeServiceLogin right? The right is not default set for admin users. – ctescu Oct 26 '12 at 07:30
0

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.
Nipun Ambastha
  • 2,553
  • 1
  • 16
  • 26
-5

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!

Andreas
  • 5,393
  • 9
  • 44
  • 53