c# - 在 C# 中对 boolean 变量进行序列化反序列化的最简单方法是什么?

标签 c# serialization boolean deserialization

当我停止并重新启动程序时,我有一个标志类(和一个更改标志值的简单按钮)我想查看更改后的 boolean 变量。我对它进行了搜索,但我有点迷路了。 现在我有一个构造函数,但我不知道如何将它与保存/加载函数一起使用。

最简单的方法是什么?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Collections;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.Xml.Serialization;
using System.Xml;
namespace SaveReloadDeneme
{
    [Serializable]
    public partial class Form1 : Form
    {
        bool flag;
        bool flag2;
        public Form1()
        {
            InitializeComponent();
            flag = false;
            flag2 = false;
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            flag = true;
            flag2 = true;
            Console.WriteLine("Flag changed: " + flag);
            Console.WriteLine("Flag2 changed: " + flag2);
        }



        void SaveData()
        {
            // Create a hashtable of values that will eventually be serialized.
            Hashtable addresses = new Hashtable();
            addresses.Add(1, flag);


            // To serialize the hashtable and its key/value pairs,   
            // you must first open a stream for writing.  
            // In this case, use a file stream.
            FileStream fs = new FileStream("DataFile.dat", FileMode.Create);

            // Construct a BinaryFormatter and use it to serialize the data to the stream.
            BinaryFormatter formatter = new BinaryFormatter();
            try
            {
                formatter.Serialize(fs, addresses);
            }
            catch (SerializationException e)
            {
                Console.WriteLine("Failed to serialize. Reason: " + e.Message);
                throw;
            }
            finally
            {
                fs.Close();
            }
        }


        public void LoadData()
        {
            // Declare the hashtable reference.
            Hashtable addresses = null;

            // Open the file containing the data that you want to deserialize.
            FileStream fs = new FileStream("DataFile.dat", FileMode.Open);
            try
            {
                BinaryFormatter formatter = new BinaryFormatter();

                // Deserialize the hashtable from the file and  
                // assign the reference to the local variable.
                addresses = (Hashtable)formatter.Deserialize(fs);
            }
            catch (SerializationException e)
            {
                Console.WriteLine("Failed to deserialize. Reason: " + e.Message);
                throw;
            }
            finally
            {
                fs.Close();
            }
           }
        private void Button2_Click(object sender, EventArgs e)
        {

        }
}

static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
            Form1 f = new Form1();
            f.SaveData();
            f.LoadData(); 

        }

当我打开 .dat" 文件时,它是乱码,我认为不包含当前保存的标志值。

最佳答案

序列化和反序列化看起来不错。但是在您的 Main() 方法中,您首先要保存 数据,然后重新加载 保存的(默认)数据。所以你覆盖了保存的数据。
调整您的代码将提供:

static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);        
        Form1 newForm = new Form1();           
        newForm.LoadData(); 
        Application.Run(newForm);
    }

您还可以在更改之前保存数据。 SaveData() 方法在关闭应用程序之前会更好。因此扩展或以前的代码将是:

static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);  
    Form1 newForm = new Form1();           
    newForm.LoadData(); 
    Application.ApplicationExit += (o, e) => newForm.SaveData();      
    Application.Run(newForm );
}

关于c# - 在 C# 中对 boolean 变量进行序列化反序列化的最简单方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57074820/

相关文章:

java - 测试类的序列化

java - 求两个矩阵的 boolean 积

c# - 多个线程使用的 Post Sharp 属性中的唯一记录器

c# - 将空 XML 元素呈现为父元素

c# - 从文件加载证书文件

c# - 需要viber webservice或api地址

java - Spring MVC中JSON body如何转换为POJO

java - 对象序列化查询和类路径

javascript - 测试字符串是否每个字母两侧都有 '+' 符号

c - Char 和 Boolean Expression 相加的结果是 Boolean 结果吗?