C# 配置文件保存为dat文件

使用到了ESBasic.dll,下载地址https://download.csdn.net/download/letunihao/10282332

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace WindowsFormsApplication1
{
    /// <summary>
    /// 系统设置。保存用户设置的数据。
    /// </summary>
    // 用于序列化的对象必须设置 [System.Serializable] 标签,该标签指示一个类可以序列化
    [Serializable]
    public class SystemSettings
    {
        public static string SystemSettingsFilePath = Application.StartupPath + "SystemSettings.dat";


        // 定义一个静态变量来保存类的实例
        private static SystemSettings singletonSettings;
        /// <summary>
        /// 单例模式,定义公有方法提供一个全局访问点。
        /// </summary>
        public static SystemSettings SingletonSettings
        {
            get
            {
                // 如果类的实例不存在则创建,
                if (singletonSettings == null)
                {
                    singletonSettings = SystemSettings.Load();
                    if (singletonSettings == null)
                    {
                        singletonSettings = new SystemSettings();
                    }
                }


                return singletonSettings;
            }
        }


        // 定义私有构造函数,使外界不能创建该类实例
        private SystemSettings() { }


        #region AutoRun
        private bool autoRun = false;
        /// <summary>
        /// 是否开机自动 运行
        /// </summary>
        public bool AutoRun
        {
            get { return autoRun; }
            set { autoRun = value; }
        }
        #endregion


        #region MacServerIP
        private string macServerIP = "http://";
        /// <summary>
        /// 是否开机自动 运行
        /// </summary>
        public string MacServerIP
        {
            get { return macServerIP; }
            set { macServerIP = value; }
        }
        #endregion


        #region ExportCodelong
        private int exportCodelong = 24;
        /// <summary>
        /// 是否开机自动 运行
        /// </summary>
        public int ExportCodelong
        {
            get 
            {
                if (this.exportCodelong == 0)
                {
                    this.exportCodelong = 7;
                }
                return exportCodelong; 
            }
            set { exportCodelong = value; }
        }
        #endregion


        public void Save()
        {
            byte[] data = ESBasic.Helpers.SerializeHelper.SerializeObject(this);
            ESBasic.Helpers.FileHelper.WriteBuffToFile(data, SystemSettingsFilePath);
        }


        private static SystemSettings Load()
        {
            try
            {
                if (!File.Exists(SystemSettingsFilePath))
                {
                    return null;
                }


                byte[] data = ESBasic.Helpers.FileHelper.ReadFileReturnBytes(SystemSettingsFilePath);
                return (SystemSettings)ESBasic.Helpers.SerializeHelper.DeserializeBytes(data, 0, data.Length);
            }
            catch (Exception ee)
            {
                System.Windows.Forms.MessageBox.Show(ee.Message);
                return null;
            }
        }
    }
}


使用方法

MessageBox.Show(SystemSettings.SingletonSettings.AutoRun.ToString());
SystemSettings.SingletonSettings.AutoRun = true;
SystemSettings.SingletonSettings.Save();

下面这个默认参数用处不大,首次调用的时候,必须通过set设置一个值

private int exportCodelong = 24;

或者在get里增加以下代码,但是并不会保存到配置文件中

get 
            {
                if (this.exportCodelong == 0)
                {
                    this.exportCodelong = 24;
                }
                return exportCodelong; 
            }




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

huangle63

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值