Click here to Skip to main content
15,887,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
My program crashes when I run it for the first time and don't have the file created and try to create it in the app.

string folderName = @"c:\consoleRace";
string saveFile = "saveFile.txt";

System.IO.Directory.CreateDirectory(folderName);

string filePath = System.IO.Path.Combine(folderName, saveFile);

if (!File.Exists(filePath))
{
    File.Create(filePath);
    List<string> output = new List<string>();
    output.Add($"{Person.name},{Person.carName},{Person.carMaxSpeed},{Person.coins}");
    File.WriteAllLines(filePath, output);
}
List<string> lines = File.ReadAllLines(filePath).ToList();

foreach (var line in lines)
{
    string[] saveInfo = line.Split(',');
    Person player = new Person();

    Person.name = saveInfo[0];
    Person.carName = saveInfo[1];
    Person.carMaxSpeed = int.Parse(saveInfo[2]);
    Person.coins = int.Parse(saveInfo[3]);
}



I think it crashes at the bold marked part and I get an error saying that it's being used in another program. Anyway I could fix this? (this is a Console application)

What I have tried:

I don't know what I can try. So if someone possibly could explain.
Posted
Updated 7-Oct-19 3:48am

You do not need to create the file before using WriteAllLines.
It is possible you run into a race condition by using WriteAllLines just after Create.
You could try:
C#
if (!File.Exists(filePath))
{
   List<string> output = new List<string>();
   output.Add($"{Person.name},{Person.carName},{Person.carMaxSpeed},{Person.coins}");
   File.WriteAllLines(filePath, output);
}


[Edit] Some more precisions:
Usually, you use File.Create when you want to get a FileStream object (which is the return type of the Create method) that you reuse later in your code. When you do so, it is a good idea to use it in a using block, so that system resources are properly disposed at the end.
C#
using (FileStream stream = File.Create(...))
{
   // Do something with the stream
}

Whereas File.WriteAllLines does not return anything, it just opens or creates the file, writes to it, and then closes it.

Hope this makes sense.
 
Share this answer
 
v2
We can't tell because we can't run your code under the same circumstances you can - personally, I'd start by looking at the error message when it crashes, and see why it says there is a problem. I'd suspect it's probably related to the Person variable, and It's content - which is possibly null

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900