代码之家  ›  专栏  ›  技术社区  ›  jp2code

使用Visual Studio 2008的64位Windows 7上的序列化问题

  •  2
  • jp2code  · 技术社区  · 15 年前

    在Visual Studio 2008中,是否有人在64位Windows 7上遇到序列化问题?

    我可以在退出之前序列化我的对象,但当我打开对象备份时,我更改的数据不在那里。

    我所知道的唯一区别是我使用的是Windows7 64位(以前是win xp 32位)。

    我下面的代码工作正常,没有触发任何异常。不过,如果我更改对象数据的值并对其进行序列化,当我返回并反序列化相同的数据时,它将不会向上拉。

    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;
      }
    }
    

    编辑(2010年6月25日上午9点,美国中部标准时间): 使用 杰德汉 的建议,我把 static void Settings(object objModel, StoreOption value); 方法分为以下两种方法。

    这个程序仍然无法回忆我对对象所做的更改。我可以修改发送到的数据 Serialize 没有错误。我的文件中的数据是否正确序列化?我不知道,因为我不知道如何在像记事本这样的文本查看器中解释数据。但是,每当我使用 Deserialize 方法,返回的数据不是我保存的数据!

    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 回复  |  直到 15 年前
        1
  •  1
  •   jdehaan    15 年前

    通过引用传递对象模型

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

    objModel 否则将不会在读取时进行修改。见 C# Parameters 更多信息。或者,编写两个独立的读写函数,第一个函数返回 对象模型 第二个有一个作为第一个参数。

    补充:

    如果您使用的是非Express Visual Studio,请激活First Chance异常,我认为必须有对该文件的访问权限。文件时间戳是否较新?如果不是,文件实际上没有保存。

    我还将在try/catch块中包含以下代码:

    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);
    }
    

    我希望你能得到一些有用的提示。你可能会错过一次失败 File.Open .