加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c#-4.0 – 如何从Windows服务中读取控制台输出?

发布时间:2020-12-15 17:22:32 所属栏目:百科 来源:网络整理
导读:我有控制台应用程序ThirdPartyApplications.exe,我必须从 Windows服务运行. 控制台应用程序给我安排:好的,不行,错误. 我需要在我的Windows服务中捕获这些响应. 我创造了功能 using (var p = new System.Diagnostics.Process()) { p.StartInfo.UseShellExecu
我有控制台应用程序ThirdPartyApplications.exe,我必须从 Windows服务运行.
控制台应用程序给我安排:好的,不行,错误.

我需要在我的Windows服务中捕获这些响应.

我创造了功能

using (var p = new System.Diagnostics.Process())
        {
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.FileName = @"C:ThirdPartyApplications.exe";
            p.StartInfo.Arguments = "3";
            p.Start();
            string o = p.StandardOutput.ReadToEnd();

            p.WaitForExit();
        }

如何捕获此输出?

编辑

using (var p = new System.Diagnostics.Process())
        {
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.FileName = @"C:ThirdPartyBatch.bat";
            p.StartInfo.Arguments = "file.zip -user.properties";
            p.Start();
            string o = p.StandardOutput.ReadToEnd();

            p.WaitForExit();
        }

批处理文件是

@echo off
call run.bat %* 1>logout.txt 2>logerr.txt
@echo code %ERRORLEVEL%
exit /B %ERRORLEVEL%

在这里,在我的变量o中,我没有收到消息“代码10”

如果我有

@ECHO off
ECHO Hello
PAUSE

在这里,我收到消息“Hello”,但是如果我更改批处理文件

@ECHO off
call run.bat %* 1>logout.txt 2>logerr.txt
ECHO Hello
PAUSE

我没有收到消息“你好”

有帮助吗?

解决方法

How can I capture this output?

仅在不作为服务运行时.

Windows上的可执行文件必须调用特定的API(直接作为Win32应用程序或间接在.NET之类的框架下),如果它不调用这些(例如基于命令行switch1的逻辑),那么它是一个普通的可执行文件.

当作为服务运行时,可执行文件由服务控制管理器(SCM)启动,并且无法访问标准输出,错误或输入.

1这在开发和调试服务时非常有用:一种“交互式”模式,允许从命令行或调试器中轻松运行.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读