代码都在,同一个类(主窗体)里是保存和读取成功的,但把 SaveInfo LoadInfo 方法放到另一个类中的时候运行读取不到数据,不知道是没有保存到还是读取的问题。但在一个窗体中又正常我晕了。到底哪里错了。 private void 大话游戏币5173_FormClosing(object sender, FormClosingEventArgs e)
{
InfoManage InfoManager = new InfoManage(double.Parse(txtLookPrice1.Text), double.Parse(txtLookPrice2.Text), double.Parse(txtLookPrice3.Text), double.Parse(txtLookPrice4.Text));
infoManage.Save();
//SaveInfo();
} public aaaaaa()
{
InitializeComponent();
if (File.Exists("User.Game"))
{
infoManage.Load();
//LoadInfo();
txtLookPrice1.Text = saveInfo.LookPrice1.ToString();
txtLookPrice2.Text = saveInfo.LookPrice2.ToString();
txtLookPrice3.Text = saveInfo.LookPrice3.ToString();
txtLookPrice4.Text = saveInfo.LookPrice4.ToString();
}
} //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public void SaveInfo()
{
SaveInfo SaveInfo = new SaveInfo(double.Parse(txtLookPrice1.Text), double.Parse(txtLookPrice2.Text), double.Parse(txtLookPrice3.Text), double.Parse(txtLookPrice4.Text));
try
{
//定义一个文件流
FileStream fs = new FileStream("User.Game", FileMode.Create);
//实例化二进制保存方式
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs, saveInfo);
//关闭流文件
fs.Close();
}
catch (Exception Ex) { MessageBox.Show(Ex.ToString()); }
}
public void LoadInfo()
{
try
{
//定义一个文件流
FileStream fs = new FileStream("User.Game", FileMode.Open);
//实例化二进制保存方式
BinaryFormatter bf = new BinaryFormatter();
saveInfo = (SaveInfo)bf.Deserialize(fs);
fs.Close();
}
catch (Exception Ex) { MessageBox.Show(Ex.ToString()); }
}在另一个类中的代码是:
class InfoManage
{
public double txtLookPrice1;
public double txtLookPrice2;
public double txtLookPrice3;
public double txtLookPrice4;
public InfoManage() { }
public InfoManage(double lookPrice1, double lookPrice2, double lookPrice3, double lookPrice4)
{
this.txtLookPrice1 = lookPrice1;
this.txtLookPrice2 = lookPrice2;
this.txtLookPrice3 = lookPrice3;
this.txtLookPrice4 = lookPrice4;
} SaveInfo saveInfo;
public void Save()
{
saveInfo = new SaveInfo(txtLookPrice1, txtLookPrice2, txtLookPrice3, txtLookPrice4);
try
{
//定义一个文件流
FileStream fs = new FileStream("User.Game", FileMode.Create);
//实例化二进制保存方式
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs, saveInfo);
//关闭流文件
fs.Close();
}
catch (Exception Ex) { MessageBox.Show(Ex.ToString()); }
}
public void Load()
{
try
{
//定义一个文件流
FileStream fs = new FileStream("User.Game", FileMode.Open);
//实例化二进制保存方式
BinaryFormatter bf = new BinaryFormatter();
saveInfo = (SaveInfo)bf.Deserialize(fs);
fs.Close();
}
catch (Exception Ex) { MessageBox.Show(Ex.ToString()); }
}
}
{
InfoManage InfoManager = new InfoManage(double.Parse(txtLookPrice1.Text), double.Parse(txtLookPrice2.Text), double.Parse(txtLookPrice3.Text), double.Parse(txtLookPrice4.Text));
infoManage.Save();
//SaveInfo();
} public aaaaaa()
{
InitializeComponent();
if (File.Exists("User.Game"))
{
infoManage.Load();
//LoadInfo();
txtLookPrice1.Text = saveInfo.LookPrice1.ToString();
txtLookPrice2.Text = saveInfo.LookPrice2.ToString();
txtLookPrice3.Text = saveInfo.LookPrice3.ToString();
txtLookPrice4.Text = saveInfo.LookPrice4.ToString();
}
} //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public void SaveInfo()
{
SaveInfo SaveInfo = new SaveInfo(double.Parse(txtLookPrice1.Text), double.Parse(txtLookPrice2.Text), double.Parse(txtLookPrice3.Text), double.Parse(txtLookPrice4.Text));
try
{
//定义一个文件流
FileStream fs = new FileStream("User.Game", FileMode.Create);
//实例化二进制保存方式
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs, saveInfo);
//关闭流文件
fs.Close();
}
catch (Exception Ex) { MessageBox.Show(Ex.ToString()); }
}
public void LoadInfo()
{
try
{
//定义一个文件流
FileStream fs = new FileStream("User.Game", FileMode.Open);
//实例化二进制保存方式
BinaryFormatter bf = new BinaryFormatter();
saveInfo = (SaveInfo)bf.Deserialize(fs);
fs.Close();
}
catch (Exception Ex) { MessageBox.Show(Ex.ToString()); }
}在另一个类中的代码是:
class InfoManage
{
public double txtLookPrice1;
public double txtLookPrice2;
public double txtLookPrice3;
public double txtLookPrice4;
public InfoManage() { }
public InfoManage(double lookPrice1, double lookPrice2, double lookPrice3, double lookPrice4)
{
this.txtLookPrice1 = lookPrice1;
this.txtLookPrice2 = lookPrice2;
this.txtLookPrice3 = lookPrice3;
this.txtLookPrice4 = lookPrice4;
} SaveInfo saveInfo;
public void Save()
{
saveInfo = new SaveInfo(txtLookPrice1, txtLookPrice2, txtLookPrice3, txtLookPrice4);
try
{
//定义一个文件流
FileStream fs = new FileStream("User.Game", FileMode.Create);
//实例化二进制保存方式
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs, saveInfo);
//关闭流文件
fs.Close();
}
catch (Exception Ex) { MessageBox.Show(Ex.ToString()); }
}
public void Load()
{
try
{
//定义一个文件流
FileStream fs = new FileStream("User.Game", FileMode.Open);
//实例化二进制保存方式
BinaryFormatter bf = new BinaryFormatter();
saveInfo = (SaveInfo)bf.Deserialize(fs);
fs.Close();
}
catch (Exception Ex) { MessageBox.Show(Ex.ToString()); }
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货