Search code examples
c#text-filesread-write

Adding words from external word file to list C#


I want to be able to access words from a Microsoft word doc in my program for a hangman game. This way I will be able to just add words to the doc instead of the actual code. I was thinking maybe there was some way to get those words and add them to a list? I'm not too sure but if someone can help that would be nice.

Thank you

Edit:

Somethings I have tried include

string[] lines = System.IO.File.ReadAllLines(@"YourPathwayHere");

I've even tried other peoples code such as

List<string> woorden = new List<string>();


System.IO.StreamReader file = new System.IO.StreamReader(@"YourPathHere");

int counter = 0;

while (!file.EndOfStream)
{
     string woord = file.ReadLine();

     for (int i = 0; i < woord.Length; i++)
          counter++;

}
Console.WriteLine(counter);
file.Close();
Console.ReadKey();

But all that does is give me random keys (eg; ???????at?HWGuays??jahso???)


Solution

  • Word documents are not simple binary blocks of text. If that is the behavior you are expecting, then you most definitely need to use a regular .txt document.

    You can then simply use:

    var lines = File.ReadAllLines("mytext.txt");
    

    This will then return an array, where each element is a single line from the text document, which can be used to populate your list box.

    It technically does not need to be a .txt file, but any file that simply stores the text without all the extra binary data that a Word document is going to contain. Word documents are far more complicated than a regular text document, an entirely different animal altogether.