首页 > 其他 > 详细

实现重定向输入

时间:2017-09-25 19:29:14      阅读:309      评论:0      收藏:0      [点我收藏+]

       原来不太理解输入重定向的意思,问了度娘之后了解了输入重定向是指不使用系统提供的标准输入端口,而进行重新的指定。即:"输入重定向运算符(<)"可以处理从除键盘外的设备到Windows Vista命令的输入。输入重定向总是用于发送文本文件内容到Windows Vista命令。查找了c#如何实现输入重定向,发现了博客园里的这么一篇文章对这次任务的实现有帮助: http://www.cnblogs.com/tdskee/p/5852723.html

      其中讲到当 Process 将文本写入其标准流中时,通常将在控制台上显示该文本。通过重定向 StandardOutput 流,可以操作或取消进程的输出。里面的语句是:process.StartInfo.RedirectStandardInput = true;  // 重定向输入流 。考虑到原来的代码在实现功能二方面是没有问题的,重定向应该是在前面加一下规定重定向输入的代码,即:if(是重定向输入)      {执行原来的代码;}

所以我打算在原有代码上进行修改。查询了ProcessStartInfo 类 https://msdn.microsoft.com/zh-cn/library/system.diagnostics.process.startinfo.aspx

我的代码修改结果如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.ComponentModel;

namespace wf
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Equals("-s"))
            {
                if (Program.StartInfo.RedirectStandardInput == true)
                {
                    StreamReader ioInput = null;
                    ioInput = new StreamReader(Console.OpenStandardInput());
                    string textFileName;
                    Console.Write(">type ");
                    textFileName = Console.ReadLine();
                    StreamReader sreader = new StreamReader(@"C:\Users\dell-pc\desktop\test2.txt");
                    string afile = sreader.ReadToEnd();
                    afile = afile.ToLower();//转换为小写字母
                    //将空格跟标点符号定义出来
                    char[] c = {  , ,, ., ?, !, :, ;, \‘, \" };
                    //分隔char[c]后产生的数组
                    string[] S = afile.Split(c);
                    //建立哈希表
                    Hashtable ha = new Hashtable();
                    for (int i = 0; i < S.Length; i++)
                    {
                        if (ha.ContainsKey(S[i]))
                        {
                            ha[S[i]] = (int)ha[S[i]] + 1;
                        }
                        else
                        {
                            ha.Add(S[i], 1);
                        }
                    }
                    string[] arrKey = new string[ha.Count];//存键
                    int[] arrValue = new int[ha.Count];//存值
                    ha.Keys.CopyTo(arrKey, 0);
                    ha.Values.CopyTo(arrValue, 0);
                    Console.WriteLine();
                    Console.WriteLine(">wf -s test.txt");
                    Console.WriteLine("total " + ha.Count);
                    Console.WriteLine();
                    Array.Sort(arrValue, arrKey);//排序
                    int n = 0;
                    for (int i = arrKey.Length - 1; i >= 0; i--)
                    {
                        if ((string)arrKey[i] != "")
                        {
                            if (n < 10)
                            {
                                //输出前10位多的单词,右对齐
                                Console.Write(arrKey[i].ToString().PadRight(12));
                                Console.WriteLine(arrValue[i].ToString());
                                n = n + 1;
                            }
                        }
                    }
                }
            }
        }
    }
}

技术分享

编译一下,却报错了报错如上图:

我很绝望。。。再去查一下资料。。。

 

实现重定向输入

原文:http://www.cnblogs.com/yuanyue-nenu/p/7593434.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!