本文介绍了如何在C#.NET中从Windows服务运行外部exe?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



Hello,

How to run external exe from windows service in c# .net?





我想运行外部exe Windows服务,但它有关即时编译的错误。



我在Windows服务中尝试过此代码。



请帮帮我。

先谢谢。



Ankit Agarwal

软件工程师



我尝试过:



protected override void OnStart(string [ ] args)

{

尝试

{

//Debugger.Break();

TraceService(Start Service++ DateTime.Now.ToString());

//timer1.Interval = 1000;



timer1.Elapsed + = new ElapsedEventHandler(OnElaps edTime);

timer1.Enabled = true;

//Debugger.Break();

}

catch(Exception ex)

{

TraceService(错误:+ ex);

//EventLog.WriteEntry(错误: + ex.Message);

}

}

private void bw_DoWork(对象发送者,DoWorkEventArgs e)

{



流程proc =新流程();

//字符串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(对象源,ElapsedEventArgs e)

{

bool connection = NetworkInterface.GetIsNetworkAvailable();

if(connection == true)

{

BackgroundWorker bw = new BackgroundWorker();

bw.DoWork + = new DoWorkEventHandler(bw_DoWork);

bw.RunWorkerAsync();

}

}



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();
}
}

推荐答案




这篇关于如何在C#.NET中从Windows服务运行外部exe?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-17 17:18