捕获控制台流输入

发布于 2024-09-26 12:19:05 字数 139 浏览 3 评论 0原文

我想制作一个读取流输入的控制台应用程序(c# 3.5)。

像这样:

dir> MyApplication.exe

该应用程序读取每一行并向控制台输出一些内容。

走哪条路?

谢谢

I would like to make a console application (c# 3.5) which reads stream input.

Like this:

dir > MyApplication.exe

The app reads each line and outputs something to the console.

Which way to go?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

美人迟暮 2024-10-03 12:19:05

您必须使用管道 (|) 将 dir 的输出通过管道传输到应用程序中。您在示例中使用的重定向 (>) 将中继文件 Application.exe 并在那里写入 dir 命令的输出,因此,损坏您的应用程序。

要从控制台读取数据,您必须使用 Console.ReadLine 方法,例如:

using System;

public class Example
{
   public static void Main()
   {
      string line;
      do { 
         line = Console.ReadLine();
         if (line != null) 
            Console.WriteLine("Something.... " + line);
      } while (line != null);   
   }
}

You have to use a pipe (|) to pipe the output of the dir into the application. Redirect (>) that you have used in your example will trunk the file Application.exe and write the output of dir command there, thus, corrupting your application.

To read the data from the console, you have to use Console.ReadLine method, for example:

using System;

public class Example
{
   public static void Main()
   {
      string line;
      do { 
         line = Console.ReadLine();
         if (line != null) 
            Console.WriteLine("Something.... " + line);
      } while (line != null);   
   }
}
困倦 2024-10-03 12:19:05

使用控制台。阅读/ReadLine 从标准输入流读取。

或者,您可以通过 控制台.In

Use Console.Read/ReadLine to read from the standard input stream.

Alternatively, you can get direct access to the stream (as a TextReader) via Console.In.

暗恋未遂 2024-10-03 12:19:05

在窗口应用程序或任何其他类型的集成中添加的做法如下:

static public void test()
{
    System.Diagnostics.Process cmd = new System.Diagnostics.Process();

    cmd.StartInfo.FileName = "cmd.exe";
    cmd.StartInfo.RedirectStandardInput = true;
    cmd.StartInfo.RedirectStandardOutput = true;
    cmd.StartInfo.CreateNoWindow = true;
    cmd.StartInfo.UseShellExecute = false;

    cmd.Start();

    /* execute "dir" */

    cmd.StandardInput.WriteLine("dir");
    cmd.StandardInput.Flush();
    cmd.StandardInput.Close();
    string line;
    int i = 0;

    do
    {
        line = cmd.StandardOutput.ReadLine();
        i++;
        if (line != null)
            Console.WriteLine("Line " +i.ToString()+" -- "+ line);
    } while (line != null);

}

static void Main(string[] args)
{
    test();
}

A practice to add in your window app or any other type of integration is as below:

static public void test()
{
    System.Diagnostics.Process cmd = new System.Diagnostics.Process();

    cmd.StartInfo.FileName = "cmd.exe";
    cmd.StartInfo.RedirectStandardInput = true;
    cmd.StartInfo.RedirectStandardOutput = true;
    cmd.StartInfo.CreateNoWindow = true;
    cmd.StartInfo.UseShellExecute = false;

    cmd.Start();

    /* execute "dir" */

    cmd.StandardInput.WriteLine("dir");
    cmd.StandardInput.Flush();
    cmd.StandardInput.Close();
    string line;
    int i = 0;

    do
    {
        line = cmd.StandardOutput.ReadLine();
        i++;
        if (line != null)
            Console.WriteLine("Line " +i.ToString()+" -- "+ line);
    } while (line != null);

}

static void Main(string[] args)
{
    test();
}
离旧人 2024-10-03 12:19:05

这实际上取决于您想要做什么以及您想要使用什么类型的流。据推测,您正在谈论读取文本流(基于“应用程序读取每一行......”)。因此,您可以执行以下操作:

    using (System.IO.StreamReader sr = new System.IO.StreamReader(inputStream))
    {
        string line;
        while (!string.IsNullOrEmpty(line = sr.ReadLine()))
        {
            // do whatever you need to with the line
        }
    }

您的 inputStream 将派生类型 System.IO.Stream(例如 FileStream)。

It really depends on what you want to do and what type of stream you want to work with. Presumably, you are talking about reading a text stream (based on "the app reads each line..."). Therefore, you could do something like this:

    using (System.IO.StreamReader sr = new System.IO.StreamReader(inputStream))
    {
        string line;
        while (!string.IsNullOrEmpty(line = sr.ReadLine()))
        {
            // do whatever you need to with the line
        }
    }

Your inputStream would derive of type System.IO.Stream (like FileStream, for example).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文