这篇是针对上一篇讲序列化的文章的一个实际案例,WinForm程序的主界面如下:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAyoAAAIcCAIAAACW/82UAAAdfUlEQVR4nO3dT6wsaVnA4V4JogREHWDCggUhg1vDQjFhaWYFK+PGRCYyJBjUBIYEopsRM3Exw9yJRkwk4x9ICBIYowHRYI6YwEnGBFg4C4WMLEYBUU/i8t477aJn+tapt+qrr6rrra4+/TzpzPSp81V1VXV31e9Wn3vP5jYAwBn7+rvfvfBtc+xNBgA4JvkFALAo+QUAsCj5BQCwKPkFADDRraK+udaVXxfVEnYgAMA4C+fXdrvdbrflKVPyq/Xlv3zq/t3tW3/2xmf/5PVf+8R9//DUT8kvoOD5Zx5+4IGHn3m+POmlqbcud/cvbz3Q4+UR3Uu5vNUcsR/VnP9Wz6JfWlJ7/MPPPN85DVijQmMlXf1q9lZle03Jrxd/+OUXf/jlF7//+bsv/Ond7z7+V4/9hPwCerXL5YEHbl12tNWty9u3Ly9v3Sq3TTOu4oIffuaZuOBGVL10995C7k1sfLsZgbcvb+3zqz0NWKPl82tfXfXtNTq/vvH0G178/ufvvvD03e8+cffbH7n73K996ndeLb+AHpe3dmnVyptdbYULVS/PEaeGb17eeuCBh595/qX/3fvuw89c9l5Zq88vV7/gVB0lv0Zd96rKr5av/dF9d7/7+N1/++id595755vvuvPP7/jjD/2o/AI6XUuply4aNa8dXbuoNGaBzQXvFvhy5+2HtQqplV8DHz66+gWn6oZc/br8mVdePPCqL731xz7/lldfXFz87eOvu/vce+988123n/2F219/2+2v3v/kB14pv4BxrhXQrmXuTSoE2T674iePxaQac/Wrbf+t3WOOykVgWN/bd9rSbsjPfv39W1/1f3/zhR/8wZNPv/k1FxcXf/m7r7nz7DvufO1td/7p/tsXr7198drH3/8K+QX06e6k3qtHjctafRep9vk1dAnq5THXF7T7AbHe/Or/kf/aTQDGmT2/bsLffPzcW179gz/8+Hcefs/H3/S6i4uLpz/y45/44Kue+s1XPvHrr3j8/a94/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et/JnLau6BmrlFPeuHl1PdYE965265fOlTehMHfgV14ylpLWMO7svNxB9eqsIGdU1KdaX7F03/zTC+/5trGzvZqDSiba02+t+IdvlM4t9UcjFIPHBOOU4NHukMevfNbeQfTaWfHAx967IlnrsGdr8OxD3E131NT2V7TVrJyrvp1mLaoCfb5NbZRyqtx4PMSv7Vw4hSelHKTTai9w51Gfv3nfDabTeHLOHEXATOuwN5qa2CuHR53Y8uoJRxutTscgHNzGvn1HzfRamvADgeAVPLraFZbA3Y4AKQ6jfx64SZabQ3Y4QCQSn4dzWprwA4HgFSnkV831bF3bbdj75VEx961AHB1dRL5BQBwk8gvAIBFyS8AgEXJLwCARckvAIBFdeQXAAB52vkFAECq/wffWr3JjACLwgAAAABJRU5ErkJggg==" alt="" />
思路:在点击保存按钮时,将标题和内容保存到集合中,自然想到应该是Dictionary<K,V>,而且用这个集合可以避免产生2个相同标题的日记。对添加到集合中的数据,采用二进制序列化到文件中,在程序执行目录中专门建一个“Notes”的文件夹存放日记文件,日记文件的文件名以标题命名。程序加载的时候,如果“Notes”文件夹下有文件,就在右侧列表中加载文件,点击时反序列化后显示在左侧。下面是具体代码:
在Form1类下定义一个集合:
Dictionary<string, string> dic = new Dictionary<string, string>();
“保存”按钮的点击操作代码如下:
var sTitle = txtTitle.Text.Trim();
var sContent = txtContent.Text.Trim(); dic.Add(sTitle, sContent); string fullPath = Path.Combine("Notes", sTitle + ".txt");
using (FileStream fsWrite=new FileStream(fullPath,FileMode.Create,FileAccess.Write))
{
//进行二进制序列化
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fsWrite, dic);
MessageBox.Show("添加日记成功");
}
在Form1_Load中的事件处理代码如下:
string sPath1 = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string sPath2 = "Notes";
string fullPath1 = Path.Combine(sPath1, sPath2);
string[] strFiles = Directory.GetFiles(fullPath1, "*.txt"); if (strFiles.Length>)
{
foreach (string item in strFiles)
{
var s = Path.GetFileName(item);
listBox1.Items.Add(s);
}
}
在listBox1_SelectedValueChanged中的事件处理代码如下:
if (listBox1.SelectedItem!=null)
{
var FileName = listBox1.SelectedItem.ToString();
var fullPath2 = Path.Combine("Notes", FileName);
using (FileStream fsRead=new FileStream(fullPath2,FileMode.Open,FileAccess.Read))
{
BinaryFormatter bf1 = new BinaryFormatter();
//进行二进制反序列化
Dictionary<string, string> dic2 = bf1.Deserialize(fsRead) as Dictionary<string, string>;
foreach (KeyValuePair<string,string> item in dic2)
{
txtTitle.Text = item.Key;
txtContent.Text = item.Value;
}
}
}
else
{
MessageBox.Show("请选择要查看的文件");
}
总结:
程序还有些Bug,因为主要的重点是放在序列化的使用上,日后如果有时间再完善功能吧。
1.对于新添加的日记,需要在下一次程序启动中才会加载到右侧listBox中(想法是做到保存后就加载到listBox);
2.对于日记的修改,修改内容后,点击保存,可适时看到修改的变化;而如果把标题和内容都修改了,程序则认为是一个全新的日记(Dictionary集合会因为key的不同,而重新添加)。