简体   繁体   English

StreamReader多行

[英]StreamReader Multiple lines

I am trying to figure out how to read multiple lines whith StreamReader. 我试图弄清楚如何通过StreamReader读取多行。 I have a text file with a list of commands inside it that I need to read from. 我有一个文本文件,里面有需要读取的命令列表。 My code works, however it will only read the first line. 我的代码有效,但是只会读取第一行。 This causes me to have to move all my commands to a single line with a space between them. 这使我不得不将所有命令移动到一行,并且在它们之间留有空格。 This is not a very tidy way of doing this seeing as I need to leave comments next to the commands. 这不是一种很整洁的方法,因为我需要在命令旁边留下注释。 Example: CONNECT: "Connects to given IP." 示例:CONNECT:“连接到给定的IP”。

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 = "";

                }
            }
        }
    }

u should empty the variable after the loop, not inside the loop 你应该在循环后而不是循环内清空变量

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 = "";
        }
    }
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 = "";

             }

        }
    }
}

try this .. use namespace using System.Net; 试试这个..使用System.Net使用名称空间;

This is what am using in one of my app and its working fine hope it'll help you out....... 这就是我的应用程序中使用的一种,它的工作效果很好,希望能对您有所帮助。

                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);
                }

you can also trim the .Split('\\n') to take all data in single line, i can't try it right now but if check it will get u out of stuck........... 您还可以修剪.Split('\\n')以将所有数据都放在单行中,我现在无法尝试,但是如果检查它可以使u摆脱卡住...........

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM