如何在C#.NET中从Windows服务运行外部exe? [英] How to run external exe from windows service in C# .NET?

查看:68
本文介绍了如何在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();
}
}

推荐答案

首先,您的服务是可能作为LocalSystem运行。该帐户无法访问网络驱动器,因此如果您的D:驱动器是网络驱动器,则LocalSystem无法看到它。您通常会创建一个专门设置的用户帐户,以便在需要访问网络资源时运行该服务。



接下来,如果您使用的是映射的驱动器号, 别。请改用UNC路径。服务不会映射任何驱动器。它们不使用登录到计算机的用户设置的驱动器映射。用户是一个单独的用户,在单独的桌面和会话下运行。用户在计算机上执行的操作对其他会话和桌面没有影响。



接下来,服务无法显示任何类型的用户界面。没有消息框,窗口,对话框,没有。



这包括服务启动的进程。该过程将启动,但由于它在桌面上独立于登录用户看到的桌面运行,因此完全登录的用户将看不到该过程。它可能会显示在任务管理器中,但该应用程序根本无法与用户桌面交互。



您永远不会说出错误消息是什么(类型的排除故障时最重要的信息!)所以不可能告诉你出了什么问题。
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.


检查一下:

Visual Studio 2010 C#中的交互式Windows服务(CSCreateProcessAsUserFromService)示例 [ ^ ]


试用此代码



流程p =新流程();

p.StartInfo.FileName =cmd.exe;

p.StartInfo.Arguments =/ c dir * .cs;

p.StartInfo.UseShellExecute = false;

p.StartInfo.RedirectStanda rdOutput = true;

p.Start();



string output = p.StandardOutput.ReadToEnd();

p.WaitForExit();





希望这有帮助!
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!


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆