Access denied when starting a service
Hi,
I want my code to automatically start a service if it's not already running when my program starts, then stop upon exit. I found the code to do this, but my program just gives me access denid errors when started. I believe I have to do some hockery pockery with tokens? although I cant find the code anywhere to do this, just wondering if anyone could help me out
thanks
Re: Access denied when starting a service
Could you give some more information. Where do you want to start the service, local or on a network device?
Give some more details about the exception, (kind of exception, innerexception)
Re: Access denied when starting a service
Hi, the service is a local service and the exception it throws is :
System.InvalidOperationException was unhandled
Message="Cannot open screen-scraper server service on computer '.'."
Source="System.ServiceProcess"
StackTrace:
at System.ServiceProcess.ServiceController.GetServiceHandle(Int32 desiredAccess)
at System.ServiceProcess.ServiceController.Stop()
at WinScraper.Program.Main() in C:\WinScraper\WinScraper\WinScraper\Program.cs:line 34
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException: System.ComponentModel.Win32Exception
Message="Access is denied"
ErrorCode=-2147467259
NativeErrorCode=5
InnerException:
as you can see it says InnerException: System.ComponentModel.Win32Exception
Message="Access is denied", so I presumed that has something to do with windows permissions?
Re: Access denied when starting a service
Please post the code you use to start the service (and use [code ] [/ code] tags).
Re: Access denied when starting a service
Check this thread for a discussion of some of the permission type issues you will run into...
Re: Access denied when starting a service
ServiceController sc = new ServiceController("screen scraper server");
if(sc.Status.Equals(ServiceControllerStatus.Stopped)){
sc.Start();
}
is the basic code I'm using to check if the service is running already, and if not then to start it. I know it's recognising the service etc, no problem getting status and things like that, just don't have the permissions to start/stop the service. Still can't find a way to do this..
Re: Access denied when starting a service
Quote:
Originally Posted by
Bobby_1234
ServiceController sc = new ServiceController("screen scraper server");
if(sc.Status.Equals(ServiceControllerStatus.Stopped)){
sc.Start();
}
is the basic code I'm using to check if the service is running already, and if not then to start it. I know it's recognising the service etc, no problem getting status and things like that, just don't have the permissions to start/stop the service. Still can't find a way to do this..
Did you follow the links in the next to last two replies in the thread I provided???