首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >StreamReader多行

StreamReader多行
EN

Stack Overflow用户
提问于 2013-05-16 12:29:06
回答 3查看 4.6K关注 0票数 1

我正在尝试弄清楚如何用StreamReader读多行。我有一个文本文件,里面有一个命令列表,我需要从中读取。我的代码可以工作,但是它只会读取第一行。这导致我不得不将所有命令移动到一行,中间有一个空格。这不是一种非常整洁的方式,因为我需要在命令旁边留下注释。示例: CONNECT:“连接到给定的IP。”

代码语言:javascript
复制
public void ConsoleEnter_KeyDown(object sender, KeyEventArgs e)
    {
        string line;
        if (e.KeyCode == Keys.Enter)
        {

            // Read the file and display it line by line.
            StreamReader file = new StreamReader("C:\\Users\\Home\\Desktop\\commands.txt");
            while ((line = file.ReadLine()) != null)
            {
                if (line.Contains(ConsoleEnter.Text))
                {
                    COMBOX.Items.Add(ConsoleEnter.Text);
                    COMBOX.Items.Remove("");
                    ConsoleEnter.Text = "";
                }
                else
                {
                    COMBOX.Items.Add("Invalid Command");
                    COMBOX.Items.Remove("");
                    ConsoleEnter.Text = "";

                }
            }
        }
    }
EN

回答 3

Stack Overflow用户

发布于 2013-05-16 12:55:03

这是我在我的一个应用程序中使用的,它工作得很好,希望它能帮助你......

代码语言:javascript
复制
                if (TxtPath.Text != string.Empty)
                 {
                  StreamReader srr = new StreamReader(TxtPath.Text);
                    try
                    {
                        ss = srr.ReadToEnd().Split('\n');
                        MessageBox.Show("File Successfully Loded in Memory \n" + TxtPath.Text, "System Manager", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    catch (Exception)
                    {

                        throw new Exception("File are not readable or write protacted");
                    }
                    LblLineCount.Text = ss.Count().ToString();
                }
                else
                {
                    MessageBox.Show("Please Browse any Log File 1st", "System Manager", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                }

你也可以修剪.Split('\n'),在一行中获取所有数据,我现在不能尝试,但如果选中它,将使你摆脱卡住.

票数 1
EN

Stack Overflow用户

发布于 2013-05-16 12:42:23

U应该在循环之后清空变量,而不是在循环内部

代码语言:javascript
复制
public void ConsoleEnter_KeyDown(object sender, KeyEventArgs e)
    {
        string line;
        if (e.KeyCode == Keys.Enter)
        {

            // Read the file and display it line by line.
            StreamReader file = new StreamReader("C:\\Users\\Home\\Desktop\\commands.txt");
            while ((line = file.ReadLine()) != null)
            {
                if (line.Contains(ConsoleEnter.Text))
                {
                    COMBOX.Items.Add(ConsoleEnter.Text);
                }
                else
                {
                    COMBOX.Items.Add("Invalid Command");
                }
            }
            COMBOX.Items.Remove("");
            ConsoleEnter.Text = "";
        }
    }
票数 0
EN

Stack Overflow用户

发布于 2013-05-16 12:45:08

代码语言:javascript
复制
public void ConsoleEnter_KeyDown(object sender, KeyEventArgs e)
 {
    string line;
    string path = @"C:\\Users\\Home\\Desktop\\commands.txt";
     WebClient client = new WebClient();
     System.IO.Stream stream = client.OpenRead(path);
     System.IO.StreamReader str = new StreamReader(stream);
         string Text=str.ReadToEnd();    
         string[] words = Text.Split(':');   


      if (e.KeyCode == Keys.Enter)
       {

       for(int i=1;i<words.Length;i++)
            {  
               if (string.compare(words[i],textBox1.text)==0)
              {
                COMBOX.Items.Add(ConsoleEnter.Text);
                COMBOX.Items.Remove("");
                ConsoleEnter.Text = "";
               }
            else
             {
                COMBOX.Items.Add("Invalid Command");
                COMBOX.Items.Remove("");
                ConsoleEnter.Text = "";

             }

        }
    }
}

试试这个..。通过System.Net使用命名空间;

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16579021

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档