Has anyone experienced issues with Serialization on Windows 7 64-bit under Visual Studio 2008?
I can Serialize my object before exiting with no errors, but when I open the object back up, my changed data is not there.
The only difference I know of is that I am on Windows 7 64-bit (previous was Win XP 32-bit).
My code below works fine, and none of the exceptions are triggered. Still, if I change the value of my object data and Serialize it, it will not pull back up when I go back and deserialize that same data.
static void Settings(object objModel, StoreOption value) {
Model obj = objModel as Model;
if (obj == null) return;
if (!Directory.Exists(ExePath)) {
Directory.CreateDirectory(ExePath);
}
string cfgFile = Path.Combine(ExePath, _CFG_FILE);
bool ok = File.Exists(cfgFile);
switch (value) {
case StoreOption.Load:
if (ok) {
try {
using (Stream stream = File.Open(cfgFile, FileMode.Open, FileAccess.Read)) {
IFormatter formatter = new BinaryFormatter();
obj = formatter.Deserialize(stream) as Model;
}
} catch (SerializationException err) {
Console.WriteLine(err);
obj = null;
}
}
break;
case StoreOption.Save:
if (ok) {
try {
File.Delete(cfgFile);
} catch (Exception err) {
Console.WriteLine(err);
}
}
if (obj == null) {
obj = new Model();
}
using (Stream stream = File.Open(cfgFile, FileMode.Create, FileAccess.Write)) {
try {
IFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, obj);
} catch (SerializationException err) {
Console.WriteLine(err);
}
}
break;
}
}
Edit (06/25/2010 @ 9 AM CST): Using jdehaan's suggestion, I split up the static void Settings(object objModel, StoreOption value);
method into the following two methods.
This routine still fails to recall changes I make to the object. I can modify the data sent to Serialize
and there are no errors. Is the data correctly serialized in my file? I don't know because I don't know how to interpret the data in a text viewer like Notepad. However, whenever I recall the saved data with the Deserialize
method, the data returned is NOT what I saved!
static Model Deserialize() {
Model obj;
if (!Directory.Exists(ExePath)) {
Directory.CreateDirectory(ExePath);
}
string cfgFile = Path.Combine(ExePath, _CFG_FILE);
bool ok = File.Exists(cfgFile);
if (ok) {
try {
using (Stream stream = File.Open(cfgFile, FileMode.Open, FileAccess.Read)) {
IFormatter formatter = new BinaryFormatter();
obj = formatter.Deserialize(stream) as Model;
}
} catch (SerializationException err) {
Console.WriteLine(err);
obj = null;
}
} else {
obj = null;
}
return obj;
}
static void Serialize(Model obj) {
if (obj == null) return;
if (!Directory.Exists(ExePath)) {
Directory.CreateDirectory(ExePath);
}
string cfgFile = Path.Combine(ExePath, _CFG_FILE);
bool ok = File.Exists(cfgFile);
if (ok) {
try {
File.Delete(cfgFile);
} catch (Exception err) {
Console.WriteLine(err);
}
}
if (obj == null) {
obj = new Model();
}
using (Stream stream = File.Open(cfgFile, FileMode.Create, FileAccess.Write)) {
try {
IFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, obj);
} catch (SerializationException err) {
Console.WriteLine(err);
}
}
}