Click here to Skip to main content
15,889,909 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

C#
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.

Please help me.
Thanks in Advance.

Ankit Agarwal
Software Engineer

What I have tried:

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();
}
}
Posted
Updated 30-Aug-16 11:08am
Comments
[no name] 30-Aug-16 14:57pm    
Let me see if I understand this since you did not explain anything about your problem. You are trying to run a service, that has no user interface and attempting to run an application that has a user interface when services are prohibited from displaying user interfaces? Are you seeing what the problem is?

First, your service is probably running as LocalSystem. That account doesn't have any access to network drives, so if your "D:" drive is a network drive, LocalSystem cannot see it. You would normally create a user account that is specifically setup to run the service if it needs access to network resources.

Next, if you're using a mapped drive letter, don't. Use UNC paths instead. Services will not have any drives mapped. They do NOT use the drive mapping set by the user that's logged into the machine. The user is a separate user and is running under a separate desktop and session. What one users does on a machine have no effect on other sessions and desktops.

Next, services can NOT show any kind of user interface. No messageboxes, windows, dialogs, NOTHING.

This INCLUDES processes launched by a service. The process will be launched, but since it's running on a desktop indepentent of the desktop the logged in user sees, the process will not be seen by the user that's logged in at all. It may show up in Task Manager but that application will not be able to interact with the user desktop at all.

You never say what the error message is (kind of the most important piece of information when troubleshooting!) so it's impossible to tell you what's going wrong.
 
Share this answer
 
Try this Code

Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/c dir *.cs";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.Start();

string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();


Hope this Helps!
 
Share this answer
 
Comments
Agarwal1984 31-Aug-16 2:39am    
This code is not working.
i have to run external my application exe.
Process p = new Process();
p.StartInfo.FileName = @"C:\Program Files (x86)\Microsoft\Notification\TaskbarNotifierDemo.exe";
p.StartInfo.Arguments = "/c dir *.cs";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.Start();

string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();

but its getting error:-
The Just-In-Time debugger was launched without necessary security permissions. To debug this process, the Just-In-Time debugger must be run as an Administrator. Would you like to debug this process?

Its not running through windows service.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900