Friday, 9 August 2013

Execute shell command from service

Execute shell command from service

I am trying to execute a shell command from within a C# service that I
created. However, this command does not seem to execute. As a standard
console application, it works perfectly though, so I know there is no
issue with the command itself or how it is being executed form within the
code. Can anyone tell me why this would not work? Please keep in mind I am
pretty new to C#, so this may just be a matter of my inexperience. Below
is the code from the service itself:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;
using System.Diagnostics;
using System.ServiceProcess;
using System.Threading;
namespace AdapterDisableTest
{
class Program : ServiceBase
{
//private static Timer workTimer;
static void Main(string[] args)
{
ServiceBase.Run(new Program());
}
public Program()
{
this.ServiceName = "AdapterDisableTest";
}
protected override void OnStart(string[] args)
{
base.OnStart(args);
Process myProcess = new Process();
myProcess.StartInfo.FileName = @"C:\Program
Files\Oracle\VirtualBox\VBoxManage.exe";
myProcess.StartInfo.Arguments = "controlvm test setlinkstate1
off";
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
}
protected override void OnStop()
{
base.OnStop();
//TODO: clean up any variables and stop any threads
}
}
}

No comments:

Post a Comment