views:

48

answers:

1

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);
    }
  }
}
+1  A: 

Pass your objModel by reference

static void Settings(ref object objModel, StoreOption value) { ... }

objModelwill not be modified on reading otherwise. See C# Parameters for some more information. Alternatively write two separate functions for reading and writing, the first returning an objModel the second having one as first parameter.

ADDED:

If you are using a NON Express Visual Studio, activate first chance exceptions, I think there must be something with the access rights to the file. Is the file timestamp newer`If not the file was not actually saved.

I would enclose following code in a try/catch block also:

try {
    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);
        }
    }
}
catch (Exception ex)
{
    Console.WriteLine("Exception: {0}", ex);
}

I hope that then you'll get some helpful hints. In your got you might miss a failing File.Open.

jdehaan
At first, I thought this would resolve the problem. I wrote two separate methods [void Serialize(Model obj) and Model Deserialize()], but this did not help.Further analysis: If I declare Model obj = new Model(), all of my parameters are initialized to 0, but Deserialize returns a Model instance with data. I can change the data, and call Serialize(obj), and there are no errors.I should update my post with the latest code... I'll do that now.
jp2code
I added some hints that I hope can help you... Good luck.
jdehaan