FileStream fs = new FileStream("Color.bin", FileMode.OpenOrCreate);
            BinaryFormatter bf = new BinaryFormatter();
            FormColor fc = bf.Deserialize(fs) as FormColor;//这一行无法反序列化空流
            fs.Close();
            this.BackColor = fc.Color;将反序列化代码注释后先序列化在反序列化就行.现在我就不知道如何判断一个文件流是否为空..

解决方案 »

  1.   

    FileStream fs = new FileStream("Color.bin", FileMode.OpenOrCreate);
      BinaryFormatter bf = new BinaryFormatter();
    if(fs.Length<1)
    {
      continue;
    }
      FormColor fc = bf.Deserialize(fs) as FormColor;//这一行无法反序列化空流
      fs.Close();
      this.BackColor = fc.Color;
      

  2.   

    Color c = DefaultColor;//默认颜色
    try{
    FileStream fs = new FileStream("Color.bin", FileMode.OpenOrCreate);
    if(fs.Length>0){
    BinaryFormatter bf = new BinaryFormatter();
    FormColor fc = bf.Deserialize(fs) as FormColor;
    c=fc.Color;
    }
    }catch{/*考虑怎么处理异常*/}
    this.BackColor = fc.Color;