如何在命令行中执行命令? [英] How to execute commands in Command Line?

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

问题描述

大家好,

我想从C#运行命令行应用程序。当我启动应用程序时,它将进入特定模式。在那之后,我必须给

命令来使用该应用程序。

如何在C#中完成这个....


谢谢和问候,

S.Madhanmohan

Hi All,
I want to run a command line appplication from C#. When i start
the application, it will go into specific mode. After that, i have to give
commands to use the application.
How Can This Be Done in C#....

Thanks and Regards,
S.Madhanmohan

推荐答案

Madhanmohan S< er ****** @ hotmail.com>写道:
Madhanmohan S <er******@hotmail.com> wrote:
我想从C#运行命令行应用程序。当我启动应用程序时,它将进入特定模式。在那之后,我必须给出
命令来使用该应用程序。
如何在C#中完成....
I want to run a command line appplication from C#. When i start
the application, it will go into specific mode. After that, i have to give
commands to use the application.
How Can This Be Done in C#....




看看System.Diagnostics.Process类及其Start方法。要将
写入应用程序的输入,请查看Process.StandardInput。如果您还有其他问题,请提供更多详细信息。


-

Jon Skeet - < sk * **@pobox.com>
http://www.pobox.com/ 〜双向飞碟

如果回复小组,请不要给我发邮件



Look at the System.Diagnostics.Process class and its Start method. To
write to the application''s input, look at Process.StandardInput. If you
have any further questions, give a bit more detail.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too




我有应用程序将在命令行模式下运行。当我启动应用程序时,它将进入特定模式(类似于,当我们使用

OSql for MSDE时)。在这种模式下,我们必须给出不同的命令来执行我们的任务。我尝试使用StandardInput方法。但是我无法在应用程序模式下执行命令。

当我使用StandardInputMethod传递命令时,C#

应用程序挂起。

你能指出一些资源或示例代码吗???

" Jon Skeet" < SK *** @ pobox.com>在消息中写道

新闻:MP ************************ @ news.microsoft.com ...
Hi,
I have application which will run in commandline mode. When i start
the application, it will go to a specific mode (Similar like, when we use
OSql for MSDE). In this mode, we have to give different commands to execute
our tasks. I tried with StandardInput Method. But i was not able to execute
the commands, in the application mode.
When i use the StandardInputMethod to pass the commands, the C#
application hangs.
Can you point some resources or sample code for the same???
"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ...
Madhanmohan S< er ****** @ hotmail.com>写道:
Madhanmohan S <er******@hotmail.com> wrote:
我想从C#运行命令行应用程序。当i
启动应用程序时,它将进入特定模式。在那之后,我必须给
命令来使用该应用程序。
如何在C#中完成....
I want to run a command line appplication from C#. When i start the application, it will go into specific mode. After that, i have to give commands to use the application.
How Can This Be Done in C#....



查看System.Diagnostics .Process类及其Start方法。要写入应用程序的输入,请查看Process.StandardInput。如果您还有其他问题,请提供更多详细信息。

-
Jon Skeet - < sk *** @ pobox.com>
< a rel =nofollowhref =http://www.pobox.com/~skeettarget =_ blank> http://www.pobox.com/~skeet
如果回复对于小组,请不要给我发邮件



Look at the System.Diagnostics.Process class and its Start method. To
write to the application''s input, look at Process.StandardInput. If you
have any further questions, give a bit more detail.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too



Madhanmohan S< er ****** @ hotmail.com>写道:
Madhanmohan S <er******@hotmail.com> wrote:
我有应用程序将在命令行模式下运行。当我启动应用程序时,它将进入特定模式(类似于,当我们使用OSql for MSDE时)。在这种模式下,我们必须提供不同的命令来执行我们的任务。我尝试使用StandardInput方法。但是我无法在应用程序模式下执行命令。
当我使用StandardInputMethod传递命令时,C#
应用程序挂起。


您是否正在阅读StandardOutput和StandardError?也许

其他进程是阻塞的。

你能指出一些资源或示例代码吗?
I have application which will run in commandline mode. When i start
the application, it will go to a specific mode (Similar like, when we use
OSql for MSDE). In this mode, we have to give different commands to execute
our tasks. I tried with StandardInput Method. But i was not able to execute
the commands, in the application mode.
When i use the StandardInputMethod to pass the commands, the C#
application hangs.
Are you reading from StandardOutput and StandardError? Perhaps the
other process is blocking.
Can you point some resources or sample code for the same???




当然。首先,一个非常简单的回声程序:


使用系统;


公共类Echo

{

static void Main()

{

string line;

while((line = Console.ReadLine())! =" quit")

Console.WriteLine(" Echo:{0}",line);

}

}


编译它(作为命令行应用程序的echo.exe)并运行它 - 基本上

它会回复你键入的内容,直到你键入quit ;


现在这里有一些自动化的东西:


使用System;

使用System。诊断;

使用System.IO;

使用System.Threading;


公共类AutoEcho

{

static void Main()

{

ProcessStartInfo psi = new ProcessStartInfo(" echo.exe");

psi.RedirectStandardOutput = true;

psi.RedirectStandardInput = true;

psi.UseShellExecute = false;

psi.Creat eNoWindow = true;

流程proc = Process.Start(psi);


//从标准输出开始一个新线程

ProcessOutputReader por = new ProcessOutputReader(proc);

por.Start();


proc.StandardInput.WriteLine(" Hello") ;

proc.StandardInput.WriteLine(" There");

proc.StandardInput.WriteLine(" quit");

}


类ProcessOutputReader

{

流程处理;


public ProcessOutputReader(Process proc)

{

this.proc = proc;

}


public void Start( )

{

新主题(新的ThreadStart(ReadAll))。开始();

}


void ReadAll()

{

StreamReader reader = proc.StandardOutput;


string line;


while((line = reader.ReadLine())!= null)

Console.WriteLine(" Process output:{0}" ;,行);

}

}

}

-

Jon Skeet - < sk *** @ pobox.com>
http:/ /www.pobox.com/~skeet

如果回复小组,请不要给我发邮件



Sure. First, a very simple echo program:

using System;

public class Echo
{
static void Main()
{
string line;
while ((line=Console.ReadLine())!="quit")
Console.WriteLine ("Echo: {0}", line);
}
}

Compile it (to echo.exe, as a command-line app) and run it - basically
it echoes what you type until you type "quit".

Now here''s something to automate it:

using System;
using System.Diagnostics;
using System.IO;
using System.Threading;

public class AutoEcho
{
static void Main()
{
ProcessStartInfo psi = new ProcessStartInfo("echo.exe");
psi.RedirectStandardOutput=true;
psi.RedirectStandardInput=true;
psi.UseShellExecute=false;
psi.CreateNoWindow=true;
Process proc = Process.Start (psi);

// Start a new thread to read from its standard output
ProcessOutputReader por = new ProcessOutputReader (proc);
por.Start();

proc.StandardInput.WriteLine ("Hello");
proc.StandardInput.WriteLine ("There");
proc.StandardInput.WriteLine ("quit");
}

class ProcessOutputReader
{
Process proc;

public ProcessOutputReader (Process proc)
{
this.proc = proc;
}

public void Start()
{
new Thread (new ThreadStart(ReadAll)).Start();
}

void ReadAll()
{
StreamReader reader = proc.StandardOutput;

string line;

while ((line = reader.ReadLine())!=null)
Console.WriteLine ("Process output: {0}", line);
}
}
}
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


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

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