Cdapimp
Cdapimp

Reputation: 9

Load List<string> from .txt

var wordsToHighlight = new List<string>() { "sign ID1:", "user:", "autohotkey", "bloody" };

if (!string.IsNullOrWhiteSpace(richTextBox1.Text))
    foreach (var word in wordsToHighlight)
        int index = 0;

        while (index != -1)
            richTextBox1.SelectionColor = Color.Red;
            index = richTextBox1.Find(word, index + word.Length - 1, richTextBox1.TextLength, RichTextBoxFinds.None);

I am currently using this code to search and highlight words in a richtextbox. In the same folder as the program, Can you use a .txt file of words to replace { "The", "is", "what", "story" } and use those words from the .txt file to search with.

Example: (The words in the .txt file are the following)

Line 1 = The
Line 2 = is
Line 3 = what
Line 4 = story

Upvotes: 0

Views: 209

Answers (1)

Bernd Fischer
Bernd Fischer

Reputation: 96

You can use File.ReadAllLines() to read the lines of your file into a string[]. This may then be converted to a List<string>, but you needn't even bother - arrays are IEnumerable, so you can do

foreach (var word in File.ReadAllLines(path_to_wordlist))
{
  //...
}

Upvotes: 3

Related Questions