Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Restart NT service through web page?

Status
Not open for further replies.

theomen

Programmer
Jun 2, 2004
158
GB
Is there any way to restart an NT service through an asp.net web page? The service will be on the same server as the web page.

I have written a vb.net application which basically polls a folder for excel spreadsheets and then executes stored procedures to import the data from these spreadsheets into an sql database. However, I have a problem in which I have built a function into a web interface to change the polling interval, but for the application to pick this change up, the service would need to be restarted.
 
It is not very secure, but if you can run as a user with the right permissions you should be able to execute code such as
Code:
System.Diagnostics.Process.Start("net stop nameOfYourService")
and
System.Diagnostics.Process.Start("net start nameOfYourService")
 
Thanks for the reply.

When I put the above code into my solution, I get the following error:

System.ComponentModel.Win32Exception: The system cannot find the file specified


I've tried running the commands in the command prompt and they work fine.

I've set a new user up on my machine, given them permission to start the service (which I can do through Service Manager), and have set them up as the user for the web application, could it still be a permissions problem?
 
I've finally managed to get it working. For those that may be interested, the following is the code I used:

Code:
System.Security.Principal.WindowsImpersonationContext impersonationContext;

impersonationContext =  ((System.Security.Principal.WindowsIdentity)User.Identity).Impersonate();
		
System.ServiceProcess.ServiceController myController = new System.ServiceProcess.ServiceController("pmsdataimportv2");

myController.Start();

impersonationContext.Undo();

I had problems starting and stopping the service due to access being denied, even though I had the same user as in IIS setup as the logon account of the service. However, by impersonating this user in the code, it all worked fine.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top