I'm developing a project in language C#
where I need to use files to save the data that circulates in data structures.
It turns out that I had the whole project badly drawn, I was not programming in layers. I was previously using Lists (data structure) and to write the Data the methods below are working perfectly
public static bool GravaCarrosEstacionados(string filename,Dictionary<string,Park> carros)
{
try
{
Stream stream = File.Open(filename, FileMode.Create);
BinaryFormatter bin = new BinaryFormatter();
bin.Serialize(stream, carros);
stream.Close();
return true;
}
catch (IOException e)
{
Console.Write("ERRO:" + e.Message);
return false;
}
}
public static bool CarregarCarrosEstacionados(string fileName,string nomeP)
{
if (File.Exists(fileName))
{
try
{
Stream stream = File.Open(fileName, FileMode.Open);
BinaryFormatter bin = new BinaryFormatter();
parques = (Dictionary<string,Park>)bin.Deserialize(stream);
stream.Close();
foreach (KeyValuePair<string, Park> x in parques)
{
if (parques.ContainsKey(nomeP))
{
Console.WriteLine("Carro {0}", x.Value.Carro.Marca);
Console.WriteLine("Modelo {0}", x.Value.Carro.Modelo);
Console.WriteLine("Matricula {0}", x.Value.Carro.Matricula);
}
}
}
catch (IOException e)
{
Console.Write("ERRO:" + e.Message);
return false;
}
}
return true;
}
Obviously I applied lists instead of dictionaries during "serialize"
and "Deserialize"
. Now with the dictionary when adding a car to the system for example, I can not even save the file.
What could be happening differently?