pb 打开文件然后重新保存时
我正在创建一个加密/散列消息的程序
我有一个包含(日期,用户,加密,消息,加密消息)的列表 => Variables.oHistorique.Liste_historique。
当我只是加密消息然后保存它时工作正常,当我打开该文件时,列表包含预期的所有信息,但是如果我打开文件然后重新保存它(即使使用新名称)它是空的,列表是空的。 我感谢我能得到的所有帮助 谢谢
我已经检查过,保存时会填充列表,但打开新保存的文件时不会填充列表
//当我单击打开时
private void ouvrirToolStripMenuItem_Click(object sender, EventArgs e)
{
FileDialog location = new OpenFileDialog();
location.Filter = "Data Files (*.dat)|*.dat";
location.FilterIndex = 1;
result = location.ShowDialog();
if (result == DialogResult.OK)
{
dataGridView1.Rows.Clear();
Variables.oHistorique = (CHistorique)oSerialize.DeSerializeObject(location.FileName);
for (int i = 0; i < Variables.oHistorique.Liste_historique.ToArray().Length; i++)
{
dataGridView1.Rows.Add(Variables.oHistorique.Liste_historique[i].Date, Variables.oHistorique.Liste_historique[i].User, Variables.oHistorique.Liste_historique[i].Type, Variables.oHistorique.Liste_historique[i].Chaine, Variables.oHistorique.Liste_historique[i].ChaineCrypt);
}
dataGridView1.CurrentCell = null;
}
}
//当我单击保存时:
private void sauvgarderToolStripMenuItem_Click(object sender, EventArgs e)
{
FileDialog location = new SaveFileDialog();
location.Filter = "Data Files (*.dat)|*.dat";
location.FilterIndex = 1;
result = location.ShowDialog();
if (result == DialogResult.OK)
{
oSerialize.SerializeObject(CHistorique.GetInstance(),location.FileName);
}
}
//这是我的类序列化
class CSerialize
{
private static CSerialize Instance;
private CSerialize()
{
}
public void SerializeObject(object o, string file)
{
Stream stream = File.Open(file, FileMode.Create);
BinaryFormatter b = new BinaryFormatter();
b.Serialize(stream, o);
stream.Close();
}
public object DeSerializeObject(string file)
{
Object o;
Stream stream = File.Open(file, FileMode.Open);
BinaryFormatter b = new BinaryFormatter();
o = b.Deserialize(stream);
stream.Close();
return o;
}
public static CSerialize GetInstance()
{
if (Instance == null)
{
Instance = new CSerialize();
}
return Instance;
}
}
I am creating a program that encrypts/hashs messages
I have a list that contains (date,user,encryption, message,encryptedmessage)
=> Variables.oHistorique.Liste_historique.
When I just encrypt messages then save it works fine, when I open that file the list contains all the information expected however if I open a file then re-save it (even under a new name) it's empty, the list is empty.
I appreciate all the help i can get
Thanks
P.S
I've checked and the list is populated when I save but not when i open the newly saved file
//when i click open
private void ouvrirToolStripMenuItem_Click(object sender, EventArgs e)
{
FileDialog location = new OpenFileDialog();
location.Filter = "Data Files (*.dat)|*.dat";
location.FilterIndex = 1;
result = location.ShowDialog();
if (result == DialogResult.OK)
{
dataGridView1.Rows.Clear();
Variables.oHistorique = (CHistorique)oSerialize.DeSerializeObject(location.FileName);
for (int i = 0; i < Variables.oHistorique.Liste_historique.ToArray().Length; i++)
{
dataGridView1.Rows.Add(Variables.oHistorique.Liste_historique[i].Date, Variables.oHistorique.Liste_historique[i].User, Variables.oHistorique.Liste_historique[i].Type, Variables.oHistorique.Liste_historique[i].Chaine, Variables.oHistorique.Liste_historique[i].ChaineCrypt);
}
dataGridView1.CurrentCell = null;
}
}
//When I click on save :
private void sauvgarderToolStripMenuItem_Click(object sender, EventArgs e)
{
FileDialog location = new SaveFileDialog();
location.Filter = "Data Files (*.dat)|*.dat";
location.FilterIndex = 1;
result = location.ShowDialog();
if (result == DialogResult.OK)
{
oSerialize.SerializeObject(CHistorique.GetInstance(),location.FileName);
}
}
//Here's my class Serialize
class CSerialize
{
private static CSerialize Instance;
private CSerialize()
{
}
public void SerializeObject(object o, string file)
{
Stream stream = File.Open(file, FileMode.Create);
BinaryFormatter b = new BinaryFormatter();
b.Serialize(stream, o);
stream.Close();
}
public object DeSerializeObject(string file)
{
Object o;
Stream stream = File.Open(file, FileMode.Open);
BinaryFormatter b = new BinaryFormatter();
o = b.Deserialize(stream);
stream.Close();
return o;
}
public static CSerialize GetInstance()
{
if (Instance == null)
{
Instance = new CSerialize();
}
return Instance;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从您提供的代码中我可以看到,当您反序列化一个对象时,您将其分配给类变量的静态字段:
但是当您在保存时序列化对象时,您尝试通过
CHistorique.GetInstance()
访问它我可以猜测返回类 CHistorique 的静态字段(因为它看起来像单例模式)。但从您提供的代码中,我看不到Variables.oHistorique
和CHistorique.GetInstance()
之间有任何联系,因此问题可能出在此处。您只需序列化空变量,因此文件为空。From code you have provided I can see that when you deserialized an object you assigned it to static field of class Variables:
but when you serializing object on save you try to access it through
CHistorique.GetInstance()
which as i can guess returns static field of class CHistorique (because it looks like singleton pattern). But from the code you have provided i don't see any connection betweenVariables.oHistorique
andCHistorique.GetInstance()
so may be problem lies here. You simply serializing empty variable, so file is empty.我找到了答案
我替换
为
I found the answer
I replaced
with