1

I am trying to figure out how to read multiple lines whith 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."

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 = ""; } } } } 
1
  • This code you post doesn't work? I see it can read the next line Commented May 16, 2013 at 4:32

3 Answers 3

1

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...........

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

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

try this .. use namespace using System.Net;

7 Comments

Thank you very much! I am still really new to programming so I don't really think of everything haha!
its ok..i am also new ...the above code worked for your case or not...or you can split the text file in rows and then store all rows in one list..and then access all list elements in array....
I tried this, but it locks up the program when I enter anything into the text box. :/
ok u can do one thing....u split the file in rows...using some symbols....or as shujaat has splitted in below code by "\n"...
can u tell me what you want to compare in that string that you want to get added in combobox...i edited the code try it once..
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.