求窗体关闭事件时保存到文件的代码
求窗体关闭事件时保存到文件的代码 要求执行Formclosing时 将信息保存到文件中去我这有窗体添加图片的代码
private void addPicture()
{
this.openPictureFile.Filter = "图片文件|*.gif;*.jpg;*.bmp;*.png";
if (this.openPictureFile.ShowDialog() == DialogResult.OK)
{
if (this.treeViewShow.Nodes.Count == 0)
{
MessageBox.Show("请填入分类名称", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
if (this.treeViewShow.SelectedNode.Parent == null)
{
if (this.openPictureFile.FileNames != null)
{
for (int i = 0; i < openPictureFile.FileNames.Length; i++)
{
//将图片的路径赋给pictureUrl
string pictureUrl = openPictureFile.FileNames[i];
//将图片的名称赋给pictureName
string pictureName = this.openPictureFile.FileNames[i].Split('\\')[this.openPictureFile.FileNames[i].Split('\\').Length - 1];
//将图片的分类赋给pictureClass
string pictureClass = this.treeViewShow.SelectedNode.ToString();
//将图片的路径、分类、名称加到集合里
this.myArrayList.Add(new Picture(openPictureFile.FileNames[i], pictureClass, pictureName));
//给TreeView 中添加图片
TreeNode tmp;
tmp = new TreeNode(pictureName);
treeViewShow.SelectedNode.Nodes.Add(tmp);
treeViewShow.SelectedNode.ExpandAll();
}
}
}
else
{
this.treeViewShow.SelectedNode = this.treeViewShow.SelectedNode.Parent;
for (int i = 0; i < openPictureFile.FileNames.Length; i++)
{
//将图片的路径赋给pictureUrl
string pictureUrl = openPictureFile.FileNames[i];
//将图片的名称赋给pictureName
string pictureName = this.openPictureFile.FileNames[i].Split('\\')
[this.openPictureFile.FileNames[i].Split('\\').Length - 1];
//将图片的分类赋给pictureClass
string pictureClass = this.treeViewShow.SelectedNode.ToString();
//将图片的路径、分类、名称加到集合里
this.myArrayList.Add(new Picture(openPictureFile.FileNames[i], pictureClass, pictureName));
//给TreeView 中添加图片
TreeNode tmp;
tmp = new TreeNode(pictureName);
treeViewShow.SelectedNode.Nodes.Add(tmp);
treeViewShow.SelectedNode.ExpandAll();
}
}
//this.showName();
}
}
}
打开窗体读取文件信息的代码是
private void load()
{
//判断类别文件是不是存在,如果存在就读取数据
if ((Application.StartupPath + "\\class.dat"))
{
//创建StreamReader实例,路径是在当前运行程序的目录下
myReadClass = new (Application.StartupPath + "\\class.dat");
string infoclass = "";
//判断是不是到文件的末尾
while ((infoclass = myReadClass.ReadLine()) != "end")
{
this.treeViewShow.Nodes.Add(infoclass);
}
//关闭文件的读写
myReadClass.Close();
}
//判断图片信息文件是不是存在,如果存在就读取数据。
if ((Application.StartupPath + "\\picdata.dat"))
{
//创建StreamReader实例,路径是在当前运行程序的目录下。
myReadPicture = new (Application.StartupPath + "\\picdata.dat");
string infoPic = "";
//判断是不是到文件的末尾
while ((infoPic = myReadPicture.ReadLine()) != "end")
{
string[] myPictureMessage = infoPic.Split(',');
//以“,”分开读取的信息,并存到数组中。
//判断数组是不是等于3。
if (myPictureMessage.Length >= 3)
{
//向动态数组中加入Picture类的实例。
this.myArrayList.Add(new Picture(myPictureMessage[2],myPictureMessage[0],myPictureMessage[1]));
}
}
for (int i = 0; i < this.treeViewShow.Nodes.Count; i++)
{
for (int j = 0; j < this.myArrayList.Count; j++)
{
if (((Picture)myArrayList[j]).picClass == this.treeViewShow.Nodes[i].ToString())
{
this.treeViewShow.SelectedNode = this.treeViewShow.Nodes[i];
this.treeViewShow.SelectedNode.Nodes.Add(((Picture)myArrayList[j]).picName);
}
}
}
//关闭文件
myReadPicture.Close();
}
}
但是窗体关闭时将添加图片的信息要怎么写啊 希望各位大大帮帮我