C# 配置文件保存为dat文件
2018-03-12 17:27
399 查看
使用到了ESBasic.dll,下载地址https://download.csdn.net/download/letunihao/10282332using 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;
}
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;
}
相关文章推荐
- C# 配置文件保存为dat文件
- c#读取ini配置文件、将配置数据保存至ini文件
- C# 读取保存App.config配置文件的完整源码参考(转)
- C# 修改保存 配置文件的两种方式以及区别
- c#通过xml保存程序配置文件
- C#实现把用户第一次选择的路径保存到配置文件app.config中
- C# 读取保存App.config配置文件的完整源码参考
- C# 读取保存App.config配置文件的完整源码参考
- !!无须定义配置文件中的每个变量的读写操作,以下代码遍历界面中各个c#控件,自动记录其文本,作为配置文件保存
- c# 使用 静态类+xml序列化 保存配置文件
- C# 如何使用配置文件保存应用程序里的配置数据
- C# 读取保存App.config配置文件的完整源码参考
- C# 读取保存App.config配置文件的完整源码参考
- 【C#】Config配置文件的读写,及无法写入/保存配置文件的问题
- C# XML配置文件读写类(用于程序配置保存)
- C#中配置文件的使用方法(连接字符串的保存)
- C# 读取保存App.config配置文件
- C#针对xml基本操作及保存配置文件应用实例
- C#中配置文件保存的路径
- C# 读取webconfig配置文件Appseting key值的方法