Ankit Agarwal

Ankit Agarwal

  • NA
  • 379
  • 254.1k

How to run external exe from windows service in C# .NET?

Aug 30 2016 2:38 PM
Hello,
 
How to run external exe from windows service in c# .net?
 
I want to run external exe from windows service but its getting error regarding just-in-time compilation.
I have tried this code in windows service. 
protected override void OnStart(string[] args)
{
try
{
//Debugger.Break();
TraceService("Start Service" + " " + DateTime.Now.ToString());
//timer1.Interval = 1000;

timer1.Elapsed += new ElapsedEventHandler(OnElapsedTime);
timer1.Enabled = true;
//Debugger.Break();
}
catch (Exception ex)
{
TraceService("Error: " + ex);
//EventLog.WriteEntry("Error: " + ex.Message);
}
}
private void bw_DoWork(object sender, DoWorkEventArgs e)
{

Process proc = new Process();
//string DirectoryName = @"D:\TaskbarNotifierDemo.exe";
//RemoveDirectorySecurity(DirectoryName, "Administrators", FileSystemRights.ExecuteFile, AccessControlType.Deny);

proc.StartInfo = new ProcessStartInfo(@"D:\TaskbarNotifierDemo.exe");
//proc.StartInfo.UseShellExecute = true;
//proc.StartInfo.Verb = "runas";
proc.Start();
proc.WaitForExit();
base.Stop();
}
private void OnElapsedTime(object source, ElapsedEventArgs e)
{
bool connection = NetworkInterface.GetIsNetworkAvailable();
if (connection == true)
{
BackgroundWorker bw = new BackgroundWorker();
bw.DoWork += new DoWorkEventHandler(bw_DoWork);
bw.RunWorkerAsync();
}
}
 
Please help me.
Thanks in Advance.

Ankit Agarwal
Software Engineer 

Answers (7)