Allow players to save the new AllowedWords back into aqawords.txt

From TRCCompSci - AQA Computer Science
Revision as of 14:35, 20 November 2017 by Admin (talk | contribs) (Created page with "The first thing you should do is find the method which loads the AllowedWords: <syntaxhighlight lang=c#> private static void LoadAllowedWords(ref List<string> Allowed...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

The first thing you should do is find the method which loads the AllowedWords:

        private static void LoadAllowedWords(ref List<string> AllowedWords)
        {
            try
            {
                StreamReader FileReader = new StreamReader("aqawords.txt");
                while (!FileReader.EndOfStream)
                {
                    AllowedWords.Add(FileReader.ReadLine().Trim().ToUpper());
                }
                FileReader.Close();
            }
            catch (Exception)
            {
                AllowedWords.Clear();
            }
        }

We are going to follow this structure but change everything to write instead of reading the files. Firstly create a new method:

        private static void SaveAllowedWords(ref List<string> AllowedWords)
        {

        }

Now to setup the file writer:

        private static void SaveAllowedWords(ref List<string> AllowedWords)
        {
                StreamWriter FileWriter = new StreamWriter("aqawords.txt");


                FileReader.Close();
        }

Now we need a loop to cycle through each allowed word, and write it to the file:

        private static void SaveAllowedWords(ref List<string> AllowedWords)
        {
            StreamWriter FileWriter = new StreamWriter("aqawords.txt");

            foreach (string word in AllowedWords)
            {
                FileWriter.WriteLine(word);
            }
            FileWriter.Close();
        }