微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将批处理shell脚本输出读入C#.Net程序

对于一个项目,我正在构build一个旧的批处理脚本系统的新前端。 我必须使用Windows XP和C#.Net。 我不想触摸这个过去的十年制作的旧的后端系统。 所以我的想法是启动cmd.exe程序并在那里执行Bash脚本。 为此,我将使用.Net中的“系统”function。

但是我还需要将“批处理脚本命令行输出”读回到我的C#程序中。 我可以redirect到一个文件。 但是必须有一种方法可以将CMD.exe的标准输出转换为我的C#程序。

非常感谢你!

File.copy()到同一台机器上的networking共享是否通过networking复制文件

如何绘制一个圆形button,并在上面做标签

为Windows服务设置环境variables

.Net和Hadoop – 知道/学习什么和可用的?

孤立的存储

用WMI调用解决“拒绝访问”exception

获取计算机的最后一次唤醒时间

获取Windows上下文菜单select的位置?

记忆中密码的一般实践

从Windows移植到Mac

鉴于更新的问题。 以下是如何启动cmd.exe来运行批处理文件并在C#应用程序中捕获脚本的输出

var process = new Process(); var startinfo = new processstartinfo("cmd.exe",@"/C c:toolshello.bat"); startinfo.RedirectStandardOutput = true; startinfo.UseShellExecute = false; process.StartInfo = startinfo; process.OutputDataReceived += (sender,args) => Console.WriteLine(args.Data); // do whatever processing you need to do in this handler process.Start(); process.BeginoutputReadLine(); process.WaitForExit();

你的方式很好。 但是最后你只能得到整个输出。 脚本运行时我想要输出。 所以在这里,首先几乎是一样的,但是我扭曲了输出。 如果您遇到问题,请参阅: http : //msdn.microsoft.com/zh-cn/library/system.diagnostics.processstartinfo.aspx

public void execute(string workingDirectory,string command) { // create the processstartinfo using "cmd" as the program to be run,and "/c " as the parameters. // Incidentally,/c tells cmd that we want it to execute the command that follows,and then exit. System.Diagnostics.processstartinfo procStartInfo = new System.Diagnostics.processstartinfo(workingDirectory + "\" + "my.bat ",command); procStartInfo.WorkingDirectory = workingDirectory; //This means that it will be redirected to the Process.StandardOutput StreamReader. procStartInfo.RedirectStandardOutput = true; //This means that it will be redirected to the Process.StandardError StreamReader. (same as StdOutput) procStartInfo.RedirectStandardError = true; procStartInfo.UseShellExecute = false; // Do not create the black window. procStartInfo.CreateNowindow = true; // Now we create a process,assign its processstartinfo and start it System.Diagnostics.Process proc = new System.Diagnostics.Process(); //This is importend,else some Events will not fire! proc.EnableRaisingEvents = true; // passing the Startinfo to the process proc.StartInfo = procStartInfo; // The given function will be raised if the Process wants to print an output to consol proc.OutputDataReceived += DoSomething; // Std Error proc.ErrorDataReceived += DoSomethingHorrible; // If Batch File is finished this Event will be raised proc.Exited += Exited; }

有些东西是关闭的,但无论你有什么想法…

DoSomething是这个功能

void DoSomething(object sendingProcess,DataReceivedEventArgs outLine); { string current = outLine.Data; }

希望这可以帮助

您无法使用system功能捕获输出,您必须深入一些子流程API。

using System; using System.Diagnostics; Process process = Process.Start(new processstartinfo("bash",path_to_script) { UseShellExecute = false,RedirectStandardOutput = true }); string output = process.StandardOutput.ReadToEnd(); process.WaitForExit(); if (process.ExitCode != 0) { … /*the process exited with an error code*/ }

您似乎对是否尝试运行bash脚本或批处理文件感到困惑。 这些不是一回事。 Bash是一个unix shell,为此它存在几个Windows端口。 “批处理文件”是通常给cmd脚本的名字。 上面的代码假定你想运行一个bash脚本。 如果要运行cmd脚本,请将bash更改为cmd 。 如果要运行bash脚本,而bash.exe不在PATH上,请将bash更改为bash.exe的完整路径。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐