有没有一种方法可以过滤StreamReader中的元素? [英] Is there a way to filter elements in a StreamReader?

查看:65
本文介绍了有没有一种方法可以过滤StreamReader中的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有(并且需要)一个程序,该程序需要从字符串执行批处理文件.为此,我用 \ n 将其拆分,然后创建一个Process并将命令写入其标准输入,如下所示:

I have (and need) a program that needs to execute a batch file from a string. For that, I split it up with \n, then I create a Process and write the commands to its standard input, like this :

        var split = "@echo off\ntitle Nice!\necho Hey!\nset /p repsonse=Choice 1234 :\npause".Split('\n');

        var commands = new List<string>();
        commands.AddRange(split);

        var proc = new Process
        {
            StartInfo = new ProcessStartInfo()
            {
                FileName = "cmd",
                UseShellExecute = false,
                RedirectStandardInput = true,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                CreateNoWindow = true,
                WindowStyle = ProcessWindowStyle.Hidden
            }
        };

        proc.Start();

        /* Read the standard output and error */

        foreach (var line in split)
            proc.StandardInput.WriteLine(line);

但是,我看到此代码有一个特定的问题.

However, there are one specific problem with this code that I can see.

无论我是否在标准输出中进行检查(读取时),我在 StandardInput 中编写的命令始终会写入 StandardOutput 中,我想防止这种情况.我试图制作一个名为 commands 的列表,其中包含所有命令的列表(此处为 split ),并检查输出行是否在列表中,但无济于事.尽管进行了检查以防止输入命令出现在输出中.

Whether or not I make checks in the standard output (when reading it), the commands I write in the StandardInput always get written to the StandardOutput and I would like to prevent that. I've tried to make a list called commands that has the list of all commands (split here) and checking if the output line is in the list, but to no avail. The input commands are still shown in the output despite making checks to prevent that.

这是过滤的样子(但不起作用):

Here's how the filtering looks like (but doesn't work) :

        var reader = proc.StandardOutput; // or StandardError, the code below is from a function in my code

        new Thread(() =>
        {
            // Skip the first 2 lines because of cmd
            for (var i = 0; i < 2; i++)
                reader.ReadLine();

            string line;
            while (!reader.EndOfStream)
            {
                line = reader.ReadLine();

                // The checks that don't work except for the first and third one (dunno why)
                if (string.IsNullOrEmpty(line) || commands.Contains(line) || line.EndsWith("@echo off") || line.Contains(commands.Find(x => x.Contains(line))))
                    continue;

                Console.WriteLine(line);
            }
        }).Start();

我在这里错过了重要的事情吗?我已经仔细检查了我的代码,以确保我找不到任何错误.

Am I missing something important here? I've double-checked my code to be sure and I can't find anything wrong with my code.

推荐答案

我找到了解决方案.我发布的可复制示例可以正常工作,只是我的错,我没有测试它,因为这是我自己的代码中的问题.我想这将为我学习一个教训:始终测试您发布的代码.

I found the solution. The reproducable example I posted will work, it's just my fault that I didn't test it, because it was an issue in my own code. I guess that will learn me a lesson : to always test the code you post.

这篇关于有没有一种方法可以过滤StreamReader中的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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