Weird behaviour if I included code after File.Exists

So the EvaluateDbFile function is checking if a db file exists, if I leave it as it is now it evaluates correctly to the existence of the file, if it exists it writes "File Exists" to the console and if it doesn't I get "File Does Not Exist" but if I put in a conditional that says if the file doesn't exist run the CreateDb function it creates the file, but doesn't run the CreateDb function as the Console.Writeline in that doesn't trigger.
No description
11 Replies
Rofus-_-
Rofus-_-8mo ago
you need to modify the CreateDb function to properly create the file and then call the SetupDb function. Here's the updated code:
private static void CreateDb(string dbFileName)
{
string dbFilePath = CreateDbPathString(dbFileName);
Console.WriteLine("Creating Db File");
File.Create(dbFilePath).Close(); // Create the file and immediately close the FileStream
DatabaseService newDbSetupService = SetupDbConnection(dbFileName, mode: false);
newDbSetupService.SetupDb();
}
private static void CreateDb(string dbFileName)
{
string dbFilePath = CreateDbPathString(dbFileName);
Console.WriteLine("Creating Db File");
File.Create(dbFilePath).Close(); // Create the file and immediately close the FileStream
DatabaseService newDbSetupService = SetupDbConnection(dbFileName, mode: false);
newDbSetupService.SetupDb();
}
you call the Close method on the FileStream returned by File.Create to ensure that the file is created immediately. Then, you proceed to call the SetupDb function as expected. Now, when the file does not exist, the CreateDb function will be called and the file will be created before executing the SetupDb function.
Dante The Badger
Thanks
Rofus-_-
Rofus-_-8mo ago
np tell me if it works because i am not 100%😅
Dante The Badger
So I've updated it to this, and it works? If I open the file now it's Updated the DB and the tables exist so the .close fixed one part of the issue but I'm till confused on what's going on with my Console.Writelines not working.
No description
Rofus-_-
Rofus-_-8mo ago
maybe need a conole.readline() idk i'm eating rn hope someone else can help u
Joschi
Joschi8mo ago
Which writeline do you mean?
Dante The Badger
I've got one in the CreateDB function that isn't writing, and some in the SetupDB function.
No description
No description
Joschi
Joschi8mo ago
Did you step through that with the debugger? Put a breakpoint in your setup db and see what happens. I can't see an obvious in code reason, why those prints shouldn't be hit.
Dante The Badger
I just tried this, they don't trigger but if I open the freshly made file it's still got all teh tables
No description
No description
Joschi
Joschi8mo ago
And if you break at the start of CreateDb()?
Dante The Badger
So I switched from having the file state in the if and moved it to a Var, and this is interesting, when I clear the folder and run the debugger the file gets made before it gets to this check, so it's somehow getting made before I get to that point.
No description
Want results from more Discord servers?
Add your server
More Posts