21

I develop one window application and I also create one service. I start the service using coding in window application, but I am getting an error like cannot open window service on computer '.'

I have used below code.

ServiceController controller = new ServiceController("SeoMozScheduleService");

if (controller.Status == ServiceControllerStatus.Stopped)
{
    controller.Start();
}

If i right click on Application and click on Run as Administrator than it works fine for me...

dsolimano
  • 8,870
  • 3
  • 48
  • 63
Kartik Patel
  • 9,303
  • 12
  • 33
  • 53

7 Answers7

18

To make this automatic every time you open the application you have to add a manifest file to your solution, then update the requestedExecutionLevel node inside the file to look like this:

    <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
</requestedPrivileges>

However, changing this setting will result in the system prompting you every time to run the application as administrator if UAC is enabled.

Community
  • 1
  • 1
Ahmed Elbatt
  • 1,028
  • 1
  • 14
  • 20
  • 2
    This didn't work for me. Simply running Visual Studio or the executable as administrator resolved the issue. – Guru Josh Mar 01 '15 at 01:54
9

Go to c://Program Files/ApplicationFolder/.exe Right-click on .exe and go to Properties then go Compatibility Tab and check true to Run this Program as an administrator Level.

Djordje Nedovic
  • 559
  • 8
  • 20
Kartik Patel
  • 9,303
  • 12
  • 33
  • 53
  • 14
    I don't have enough reputation to downvote so I'll just comment here and say that elevating permissions is never the right solution because your users might not have admin privileges. – Tsury May 17 '15 at 06:08
  • 1
    I run it in a console as admin, as well the services as admin... and the service did install but it did not start until I set that property. wow, It is a relieve! thanks! – Jaider Oct 20 '16 at 13:51
  • I agree Jaider. There are a number of server situations where users aren't involved, but automated tasks are being run under a single user. That's part of the beauty of stackoverflow--multiple answers, one of which may work best in your environment. – Jeff Jan 09 '17 at 20:38
  • 1
    Just doing this in a web-api - there's no possibility to run (in my case debug) this as an admin... – Matthias Burger Oct 15 '19 at 09:40
  • @Tsury in addition to what you say, it is just bad practice to automatically use administrator privileges when lesser privileges are sufficient, right? – Sam Hobbs Mar 18 '20 at 08:27
5

if you are using visual studio then close it and re open visual sudio with run it as administrator

2

None of these solutions helped me, because it would help if I actually had ensured I had Administrator access on the target computer first! I was taking away and giving back Administrator access to my main account using my domain admin account. When I ran some ServiceController code that was checking if a service was running on a remote computer, under the domain admin account, all was fine. When I did it as my main account, that's when it didn't work - even when running Visual Studio as an Administrator. Turns out I had been testing without my main account in the local Admin group... d'oh!

vapcguy
  • 7,097
  • 1
  • 56
  • 52
1

I ran into this with a scheduled task on a server--checking the "Run with highest privileges" solved it. (The service account has to have admin rights of course for this to work.)

Jeff
  • 8,020
  • 34
  • 99
  • 157
0

Framework version change in the app.conf to the version which is installed on the system fixed the issue for me.

Abhishek Dhote
  • 1,638
  • 10
  • 41
  • 62
0

The application probably does not need to require Administrator permission. The Microsoft Management Console (MMC) (that includes the Services snap-in) does not. The manifest for the MMC has:

<requestedExecutionLevel
    level="highestAvailable"
    uiAccess="false"
/>

It does not have level="requireAdministrator".

See c# - How do I create/edit a Manifest file? - Stack Overflow for instructions for adding a manifest to the project.

There are very many articles about the principle of Least Privilege, including the following.

Sam Hobbs
  • 2,594
  • 3
  • 21
  • 32