Search code examples
c#arrayscsvstreamreader

Reading individual words into an array using streamreader C#


I am trying to get a list of words (below) to be put into an array. I want each word to be in it's own index.

Here is my code that I have so far.

string badWordsFilePath = openFileDialog2.FileName.ToString();
StreamReader sr = new StreamReader(badWordsFilePath);
string line = sr.ReadToEnd();
string[] badWordsLine = line.Split(' ');
int BadWordArrayCount = 0;
foreach (string word in badWordsLine)
{
    badWords[BadWordArrayCount] = word;
    BadWordArrayCount = BadWordArrayCount + 1;
}
int test = badWords.Length;
MessageBox.Show("Words have been imported!");
BadWordsImported = true;

Here is the list of words I want to import.

label
invoice
post
document
postal
calculations
copy
fedex
statement
financial
dhl
usps
8
notification
n
irs
ups
no
delivery
ticket

If someone could give me an example of how to get this to work, that would be a huge help.


Solution

  • Simplified code:

    string badWordsFilePath = openFileDialog2.FileName.ToString();
    string[] badWords = File.ReadAllLines(badWordsFilePath);
    int test = badWords.Length;
    MessageBox.Show("Words have been imported!");
    BadWordsImported = true;