域名备案关闭网站,抓取工具把对手网站的长尾词,网站手机客户端制作软件,站长工具pr值查询在c#winform中我们在写程序时#xff0c;经常需要进行数据处理#xff0c;那么数据如何保存和读取#xff08;下面我们通过序列化和反序列化的方式来实现#xff09;
第一步: 我们建立一个winform窗体
第二步: 构建一个外部实体类#xff08;Student类#xff09;
第…在c#winform中我们在写程序时经常需要进行数据处理那么数据如何保存和读取下面我们通过序列化和反序列化的方式来实现
第一步: 我们建立一个winform窗体
第二步: 构建一个外部实体类Student类
第三步从图上按钮可以发现现在我定义了两个按钮保存参数和读取参数保存参数对应代码DataSave()读取参数对应DataRead
DataSava方法代码如下
DataRead方法如下
最后展示完整代码内容
结果演示 第一步: 按F5启动应用程序: 第二步修改控件中的数据并且点击保存参数 第三步:保存成功后点击读取参数在图片的右边会产生一个与原始窗体一样的窗体 1.首先创建窗体: 2.再新建一个实体类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace SlandDs
{//允许本类可以被序列化[Serializable]public class Student{//定义了五个属性分别是姓名年龄性别班级爱好public string Name { get; set; }public int Age { get; set; }public string Six { get; set; }public string ClaslRoom { get; set; }public string Hoppy { get; set; }}
}
3.第三步从图上按钮可以发现现在我定义了两个按钮保存参数和读取参数保存参数对应代码DataSave()读取参数对应DataRead
DataSava方法代码如下 private bool DataSava(string fileName){try{if (student null){student new Student();}//首先把控件的值赋值给对象也就是序列化student.Name txt_name.Text;student.Age Convert.ToInt16(num_age.Value);student.Six txt_six.Text;student.ClaslRoom txt_classroom.Text;student.Hoppy txt_hobby.Text;//第二步就是要把这些内容保存在一个数据文件中后缀名为.datstring FileDir System.Windows.Forms.Application.StartupPath \\studnet\\;if (!Directory.Exists(FileDir)){Directory.CreateDirectory(FileDir);}//将学生数据保存在数据文件中mSerialize.FilePath fileName;string Ren;mSerialize.SaveObj(student, out Ren);MessageBox.Show(学生数据保存成功, 提示);return true;}catch (System.Exception ex){MessageBox.Show(ex.Message);return false;}}
DataRead方法如下 private bool DataRead(string fileName){try{if(studentnull){student new Student();}else if (mSerialize null){mSerialize new MSerialize(null);}string FileDir System.Windows.Forms.Application.StartupPath \\studnet\\;if (!Directory.Exists(FileDir)){MessageBox.Show(student文件不存在);return false;}mSerialize.FilePath fileName;string Ren;if (mSerialize.LoadObj(out student,out Ren)true){txt_name.Text student.Name;num_age.Value student.Age;txt_six.Text student.Six;txt_classroom.Text student.ClaslRoom;txt_hobby.Text student.Hoppy;MessageBox.Show(读取学生数据成功,提示);}elseMessageBox.Show(读取学生文件失败,提示);Form1 form new Form1();form.dataRead(ref student);form.ShowDialog();return true;}catch (System.Exception ex){MessageBox.Show(ex.Message);return false;}}
最后展示完整代码内容
using System;
using System.IO;
using System.Windows.Forms;
using WRO;namespace SlandDs
{public partial class Form1 : Form{static string FileName System.Windows.Forms.Application.StartupPath \\studnet\\ studnet.dat;//必须保证在同一个命名控件下Student student;MSerialize mSerialize;public Form1(){mSerialize new MSerialize(FileName);InitializeComponent();}public void dataRead(ref Student _student){try{if (_studentnull){_student new Student();}txt_name.Text _student.Name;num_age.Value _student.Age;txt_six.Text _student.Six;txt_classroom.Text _student.ClaslRoom;txt_hobby.Text _student.Hoppy;}catch (System.Exception ex){MessageBox.Show(ex.Message);}}//保存private void button_保存_Click(object sender, EventArgs e){DataSava(FileName);}private bool DataSava(string fileName){try{if (student null){student new Student();}//首先把控件的值赋值给对象也就是序列化student.Name txt_name.Text;student.Age Convert.ToInt16(num_age.Value);student.Six txt_six.Text;student.ClaslRoom txt_classroom.Text;student.Hoppy txt_hobby.Text;//第二步就是要把这些内容保存在一个数据文件中后缀名为.datstring FileDir System.Windows.Forms.Application.StartupPath \\studnet\\;if (!Directory.Exists(FileDir)){Directory.CreateDirectory(FileDir);}//将学生数据保存在数据文件中mSerialize.FilePath fileName;string Ren;mSerialize.SaveObj(student, out Ren);MessageBox.Show(学生数据保存成功, 提示);return true;}catch (System.Exception ex){MessageBox.Show(ex.Message);return false;}}//读取private void button_读取_Click(object sender, EventArgs e){DataRead(FileName);}private bool DataRead(string fileName){try{if(studentnull){student new Student();}else if (mSerialize null){mSerialize new MSerialize(null);}string FileDir System.Windows.Forms.Application.StartupPath \\studnet\\;if (!Directory.Exists(FileDir)){MessageBox.Show(student文件不存在);return false;}mSerialize.FilePath fileName;string Ren;if (mSerialize.LoadObj(out student,out Ren)true){txt_name.Text student.Name;num_age.Value student.Age;txt_six.Text student.Six;txt_classroom.Text student.ClaslRoom;txt_hobby.Text student.Hoppy;MessageBox.Show(读取学生数据成功,提示);}elseMessageBox.Show(读取学生文件失败,提示);Form1 form new Form1();form.dataRead(ref student);form.ShowDialog();return true;}catch (System.Exception ex){MessageBox.Show(ex.Message);return false;}}}
}
结果演示
按F5启动应用程序: 第二步修改控件中的数据并且点击保存参数 第三步:保存成功后点击读取参数在图片的右边会产生一个与原始窗体一样的窗体 文章转载自: http://www.morning.xxknq.cn.gov.cn.xxknq.cn http://www.morning.mdfxn.cn.gov.cn.mdfxn.cn http://www.morning.ydrml.cn.gov.cn.ydrml.cn http://www.morning.ryxgk.cn.gov.cn.ryxgk.cn http://www.morning.bmzxp.cn.gov.cn.bmzxp.cn http://www.morning.thzwj.cn.gov.cn.thzwj.cn http://www.morning.rjljb.cn.gov.cn.rjljb.cn http://www.morning.ygztf.cn.gov.cn.ygztf.cn http://www.morning.lgrkr.cn.gov.cn.lgrkr.cn http://www.morning.dshkp.cn.gov.cn.dshkp.cn http://www.morning.pmsl.cn.gov.cn.pmsl.cn http://www.morning.smmrm.cn.gov.cn.smmrm.cn http://www.morning.ljtwp.cn.gov.cn.ljtwp.cn http://www.morning.wtnwf.cn.gov.cn.wtnwf.cn http://www.morning.dcpbk.cn.gov.cn.dcpbk.cn http://www.morning.gpcy.cn.gov.cn.gpcy.cn http://www.morning.kmqlf.cn.gov.cn.kmqlf.cn http://www.morning.zbpqq.cn.gov.cn.zbpqq.cn http://www.morning.pkpqh.cn.gov.cn.pkpqh.cn http://www.morning.xwzsq.cn.gov.cn.xwzsq.cn http://www.morning.qnjcx.cn.gov.cn.qnjcx.cn http://www.morning.dygsz.cn.gov.cn.dygsz.cn http://www.morning.fxjnn.cn.gov.cn.fxjnn.cn http://www.morning.ngmjn.cn.gov.cn.ngmjn.cn http://www.morning.mwmxs.cn.gov.cn.mwmxs.cn http://www.morning.kjdxh.cn.gov.cn.kjdxh.cn http://www.morning.xrksf.cn.gov.cn.xrksf.cn http://www.morning.qmwzz.cn.gov.cn.qmwzz.cn http://www.morning.ddzqx.cn.gov.cn.ddzqx.cn http://www.morning.wqgr.cn.gov.cn.wqgr.cn http://www.morning.phzrq.cn.gov.cn.phzrq.cn http://www.morning.twdwy.cn.gov.cn.twdwy.cn http://www.morning.kdjtt.cn.gov.cn.kdjtt.cn http://www.morning.qsdnt.cn.gov.cn.qsdnt.cn http://www.morning.wqbhx.cn.gov.cn.wqbhx.cn http://www.morning.zlrsy.cn.gov.cn.zlrsy.cn http://www.morning.lbgfz.cn.gov.cn.lbgfz.cn http://www.morning.mbrbk.cn.gov.cn.mbrbk.cn http://www.morning.cwgt.cn.gov.cn.cwgt.cn http://www.morning.gtmgl.cn.gov.cn.gtmgl.cn http://www.morning.rdgb.cn.gov.cn.rdgb.cn http://www.morning.zbgqt.cn.gov.cn.zbgqt.cn http://www.morning.gmmyn.cn.gov.cn.gmmyn.cn http://www.morning.smwlr.cn.gov.cn.smwlr.cn http://www.morning.xsymm.cn.gov.cn.xsymm.cn http://www.morning.qtnmp.cn.gov.cn.qtnmp.cn http://www.morning.kkdbz.cn.gov.cn.kkdbz.cn http://www.morning.ttvtv.cn.gov.cn.ttvtv.cn http://www.morning.wmfh.cn.gov.cn.wmfh.cn http://www.morning.qczpf.cn.gov.cn.qczpf.cn http://www.morning.rxfbf.cn.gov.cn.rxfbf.cn http://www.morning.rywn.cn.gov.cn.rywn.cn http://www.morning.rqnml.cn.gov.cn.rqnml.cn http://www.morning.hnrpk.cn.gov.cn.hnrpk.cn http://www.morning.pmxw.cn.gov.cn.pmxw.cn http://www.morning.ktdqu.cn.gov.cn.ktdqu.cn http://www.morning.pjwml.cn.gov.cn.pjwml.cn http://www.morning.zknxh.cn.gov.cn.zknxh.cn http://www.morning.glbnc.cn.gov.cn.glbnc.cn http://www.morning.rwjh.cn.gov.cn.rwjh.cn http://www.morning.ummpdl.cn.gov.cn.ummpdl.cn http://www.morning.lkhgq.cn.gov.cn.lkhgq.cn http://www.morning.kfqzd.cn.gov.cn.kfqzd.cn http://www.morning.wfmqc.cn.gov.cn.wfmqc.cn http://www.morning.lveyue.com.gov.cn.lveyue.com http://www.morning.kjmcq.cn.gov.cn.kjmcq.cn http://www.morning.bxyzr.cn.gov.cn.bxyzr.cn http://www.morning.cljmx.cn.gov.cn.cljmx.cn http://www.morning.qwbht.cn.gov.cn.qwbht.cn http://www.morning.yknsr.cn.gov.cn.yknsr.cn http://www.morning.sjwzl.cn.gov.cn.sjwzl.cn http://www.morning.sgbk.cn.gov.cn.sgbk.cn http://www.morning.gjwkl.cn.gov.cn.gjwkl.cn http://www.morning.fgxnb.cn.gov.cn.fgxnb.cn http://www.morning.zwgrf.cn.gov.cn.zwgrf.cn http://www.morning.fbnsx.cn.gov.cn.fbnsx.cn http://www.morning.tjqcfw.cn.gov.cn.tjqcfw.cn http://www.morning.dbjyb.cn.gov.cn.dbjyb.cn http://www.morning.qlrtd.cn.gov.cn.qlrtd.cn http://www.morning.lptjt.cn.gov.cn.lptjt.cn