C# winform如何实现数据的保存和读取

在c#winform中我们在写程序时,经常需要进行数据处理,那么数据如何保存和读取(下面我们通过序列化和反序列化的方式来实现)

第一步: 我们建立一个winform窗体

第二步: 构建一个外部实体类(Student类)

第三步:从图上按钮可以发现现在我定义了两个按钮(保存参数和读取参数)保存参数对应代码DataSave(),读取参数对应(DataRead)

DataSava方法代码如下:

DataRead方法如下

最后展示完整代码内容

结果演示:

第一步: 按F5启动应用程序:

第二步:修改控件中的数据并且点击保存参数

第三步:保存成功后点击读取参数,在图片的右边会产生一个与原始窗体一样的窗体

1.首先创建窗体:

2.再新建一个实体类:

cs 复制代码
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方法代码如下:

cs 复制代码
        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;
                //第二步就是要把这些内容保存在一个数据文件中(后缀名为.dat)
                string 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方法如下

cs 复制代码
        private bool DataRead(string fileName)
        {
            try
            {
                if(student==null)
                {
                    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("读取学生数据成功","提示");
                }
                else
                    MessageBox.Show("读取学生文件失败","提示");
                Form1 form = new Form1();
                form.dataRead(ref student);
                form.ShowDialog();
                return true;
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
 
                return false;
            }
        }

最后展示完整代码内容

cs 复制代码
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 (_student==null)
                {
                    _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;
                //第二步就是要把这些内容保存在一个数据文件中(后缀名为.dat)
                string 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(student==null)
                {
                    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("读取学生数据成功","提示");
                }
                else
                    MessageBox.Show("读取学生文件失败","提示");
                Form1 form = new Form1();
                form.dataRead(ref student);
                form.ShowDialog();
                return true;
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
 
                return false;
            }
        }
    }
}

结果演示:

按F5启动应用程序:

第二步:修改控件中的数据并且点击保存参数

第三步:保存成功后点击读取参数,在图片的右边会产生一个与原始窗体一样的窗体

相关推荐
黄雪超3 小时前
JVM——函数式语法糖:如何使用Function、Stream来编写函数式程序?
java·开发语言·jvm
ThetaarSofVenice3 小时前
对象的finalization机制Test
java·开发语言·jvm
思则变3 小时前
[Pytest] [Part 2]增加 log功能
开发语言·python·pytest
lijingguang4 小时前
在C#中根据URL下载文件并保存到本地,可以使用以下方法(推荐使用现代异步方式)
开发语言·c#
¥-oriented4 小时前
【C#中路径相关的概念】
开发语言·c#
CoderCodingNo4 小时前
【GESP】C++四级考试大纲知识点梳理, (7) 排序算法基本概念
开发语言·c++·排序算法
ArabySide4 小时前
【WCF】通过AOP实现基于JWT的授权与鉴权的实践
c#·jwt·aop·wcf
恋猫de小郭4 小时前
Meta 宣布加入 Kotlin 基金会,将为 Kotlin 和 Android 生态提供全新支持
android·开发语言·ios·kotlin
JosieBook5 小时前
【Java编程动手学】使用IDEA创建第一个HelloJava程序
java·开发语言·intellij-idea
Thomas_YXQ5 小时前
Unity3D DOTS场景流式加载技术
java·开发语言·unity