C
C#2d ago
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();
}
}
}
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();
}
}
}
18 Replies
Ꜳåąɐȁặⱥᴀᴬ
yeah but can you post the exception message?
Kiel
Kiel2d ago
you could try using File.OpenWrite instead of just creating an empty file
Fumetsu
FumetsuOP2d ago
No description
Kiel
Kiel2d ago
using var file = File.OpenWrite(FilePath);
await using var sw = new StreamWriter(file);
...
using var file = File.OpenWrite(FilePath);
await using var sw = new StreamWriter(file);
...
iirc
Fumetsu
FumetsuOP2d ago
Oooh okay I'll have to look into using that, thanks for the help!
Unknown User
Unknown User2d ago
Message Not Public
Sign In & Join Server To View
Fumetsu
FumetsuOP2d ago
Ahhh sorry I'm quite new to this. The exception message must've not been showing because the exception was thrown at the end of the Applications lifetime Anyways Kiel's solution did indeed work, thank you guys for all the help! It seems I still need to brush up on some more documentation..
Unknown User
Unknown User2d ago
Message Not Public
Sign In & Join Server To View
Fumetsu
FumetsuOP2d ago
Ahhh that's alot of stuff to brush up on. Is there a reason why the first word is usually full lowercase? I usually prefer to have all words capitalized Yeahh I kinda just throw alot of stuff together to see if it works first, then I try and come back later to refine it
Unknown User
Unknown User2d ago
Message Not Public
Sign In & Join Server To View
Kiel
Kiel2d ago
reason? no, not really. it's about consistency and giving you an easy way to tell at a glance whether a variable name is a Property, _field, or localVariable. in my experience at least
Fumetsu
FumetsuOP2d ago
Ahhh okay yeah that makes sense
Unknown User
Unknown User2d ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX2d ago
namespace Namespace;

[Attribute]
public class Class
{
public string PublicField;
private bool _privateField;

public int PublicProperty { get; set; }

public Class() {} // Constructor

public void Method(int parameter)
{
var localVariable = parameter;

int LocalMethod(string param) { return 3; }
}
}
namespace Namespace;

[Attribute]
public class Class
{
public string PublicField;
private bool _privateField;

public int PublicProperty { get; set; }

public Class() {} // Constructor

public void Method(int parameter)
{
var localVariable = parameter;

int LocalMethod(string param) { return 3; }
}
}
Unknown User
Unknown User2d ago
Message Not Public
Sign In & Join Server To View
Fumetsu
FumetsuOP2d ago
Yeah I think I get what you're saying. Currently I have no plans for working with anyone else, but I think it would be better for me to prepare now rather than later When I get the time I'll make sure to go through my code and make it more user friendly
Unknown User
Unknown User2d ago
Message Not Public
Sign In & Join Server To View
Fumetsu
FumetsuOP2d ago
That's true actually, because clearly I need alot of help lol

Did you find this page helpful?