C#C
C#9mo ago
38 replies
Fumetsu

✅ System.IO.IOException Being Thrown

As stated above, a System.IO.IOException is being thrown. I believe it's being thrown because the Streamwriter is trying to write to a file that "doesn't exist", but I am creating the file before that code runs. Any ideas on what could be a solution? Thanks!
        private async Task SaveMessages(bool ForceSave = false)
        {
            Spark.DebugLog(ChatMessages.Count.ToString());
            if (LogMessages || ForceSave && ChatMessages.Count >= 0)
            {
                string FilePath = MessageLogPath + @"\ChatLog   " + DateTime.Now.Day + ";" + DateTime.Now.Month + ";" + DateTime.Now.Year + ".txt";

                if (!System.IO.File.Exists(FilePath))
                {
                    System.IO.File.Create(FilePath);
                }

                await using (StreamWriter sw = new StreamWriter(FilePath))
                {
                    foreach (string Message in ChatMessages)
                    {
                        sw.WriteLine(Message);
                    }
                    sw.Flush();
                }
            }
        }
Was this page helpful?