如何从命令提示符中读取 [英] How to read from command prompt

查看:94
本文介绍了如何从命令提示符中读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从命令提示符下读取信息?

它可能是命令提示符显示的任何内容.

我调用cmd.exe并像下面这样发送参数.

How to read from command prompt??

It could be any thing that command prompt shows.

I call cmd.exe and send argument like bellow.

process = new System.Diagnostics.Process();
                stratInfo = new System.Diagnostics.ProcessStartInfo();
                // stratInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                stratInfo.FileName = "cmd.exe";
                stratInfo.Arguments = "/c RUNCOB " + file_path;
                process.StartInfo = stratInfo;
                process.Start();




编译文件后,它将立即关闭,我的意思是cmd.exe正在终止.我该怎么办




After it compile the file it is immediately closing i mean cmd.exe is terminating. what should i do

推荐答案

首先,不是从命令提示符(仅)-从控制台.您需要使用System.Diagnostics.Process.Run运行某些控制台应用程序,并重定向两个流:stdoutstderr.有关代码示例,请参见System.Diagnostics.Process.StandardOutputSystem.Diagnostics.Process.StandardError的帮助: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx [ http://msdn.microsoft.com/zh-cn/library/system.diagnostics.process.standarderror.aspx [ http://msdn.microsoft.com/en-us/library /system.diagnostics.process.aspx [ http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.aspx [
First, not from command prompt (only) — from console. You need to run some console application using System.Diagnostics.Process.Run and redirect two streams: stdout and stderr. For code samples, see help on System.Diagnostics.Process.StandardOutput and System.Diagnostics.Process.StandardError: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx[^], http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standarderror.aspx[^].

As you can re-direct the output to any stream; and you can write any implementation for the abstract stream, you can re-direct output where you want: to UI (this is how compilers are embedded in IDE), to file or any other kind of stream.

For more information, see http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx[^] and help on ProcessStartInfo: http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.aspx[^].

Please see my other Answer for further detail.

—SA


回答后续问题:

好吧,看来您的应用程序应该可以作为Cobol的某种IDE.
这正是System.Diagnostics.Process的用途.

您需要使用System.Diagnostics.Process(System.Diagnostics.ProcessStartInfo).在您的情况下,像这样构造ProcessStartInfo:

Answering the follow-up Question:

All right, it looks like your application should work as some kind of IDE for Cobol.
This is exactly what System.Diagnostics.Process is used for.

You need to use System.Diagnostics.Process(System.Diagnostics.ProcessStartInfo). In your case, construct ProcessStartInfo like this:

System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(compiler, inputFile);
startInfo.RedirectStandardError = true;
startInfo.RedirectStandardOutput = true;
startInfo.StandardOutputEncoding = //change if you need, you might need it
startInfo.StandardErrorEncoding = //change if you need, you might need it
//...



在您的示例中,compiler是"RUNCOB"和inputFile,例如,"file1.cob".

使用System.Diagnostics.Process.Start的实例方法,如下所示:
http://msdn.microsoft .com/en-us/library/system.diagnostics.process.standardoutput.aspx [



Using your example, compiler is "RUNCOB" and inputFile, for example, "file1.cob".

Use instance method of System.Diagnostics.Process.Start as shown here: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx[^].

Make sure you setup output streams as shown in the code sample (see the reference above). Read the streams to the end using Process.StandardOutput.ReadToEnd and Process.StandardError.ReadToEnd. (You use instance of the Process here.)
Make sure you use System.Diagnostics.Process.Start to ensure the compilation is finished.

You can parse the compiler output string into line like this.

string streamOutput = myProcess.StandardOutput.ReadToEnd();

//...

string[] lines = streamOutput.Split(new string[] { System.Environment.NewLine, }, 
    System.StringSplitOptions.None);
//for goodness sake:
//don't use \r\n or something: this is not portable



最好为此指定一个单独的线程.您可以使用阻塞的调用System.Threading.EventWaitHandle.WaitOne保持线程运行,处于等待状态,并在每次命令文件从主线程(如果使用UI则为UI线程)中调用System.Threading.EventWaitHandle.Set进行编译时,将其解除阻塞.

由于这不是UI线程,因此不能直接使用该线程中的UI.您只能通过System.Windows.Forms.ControlSystem.Threading.Dispatcher的方法InvokeBeginInvoke使用调用.

您可以在这里找到详细的说明:
Control.Invoke()与Control.BeginInvoke() [ ^ ],
另请参见:
Treeview Scanner和MD5的问题 [



It''s best to dedicate a separate thread for that. You can keep the thread running, sitting at wait state using the blocked call System.Threading.EventWaitHandle.WaitOne and unblock it every time the file(s) is commanded for compilation calling System.Threading.EventWaitHandle.Set from your main thread (you UI thread if you use UI).

As this is not UI thread, you cannot use UI from this thread, not directly. You can only use invocation via System.Windows.Forms.Control or System.Threading.Dispatcher via their methods Invoke or BeginInvoke.

You will find detailed explanation here:
Control.Invoke() vs. Control.BeginInvoke()[^],
see also:
Problem with Treeview Scanner And MD5[^].

—SA


,使用consloe.read();你可以做到的.
by using consloe.read(); you can do it.


这篇关于如何从命令提示符中读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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