使用到了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; }