1

我试图弄清楚如何使用 StreamReader 读取多行。我有一个文本文件,其中包含我需要读取的命令列表。我的代码有效,但它只会读取第一行。这导致我必须将所有命令移动到一行,它们之间有一个空格。这不是一种非常整洁的方式,因为我需要在命令旁边留下评论。示例: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 = "";

                }
            }
        }
    }
4

3 回答 3

1

这是我的一个应用程序中使用的内容,它工作正常,希望它能帮助你......

                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') 以在单行中获取所有数据,我现在无法尝试,但如果检查它会让您摆脱卡住......

于 2013-05-16T04:55:02.957 回答
0

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

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 = "";
        }
    }
于 2013-05-16T04:42:23.160 回答
0
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 使用命名空间;

于 2013-05-16T04:45:07.977 回答