使用settings.settings来保存数据库配置等信息的。但是设置为“应用程序”级别,就没法方便的用 Properties.Settings.Default.Save() 更改保存。且没法在程序不重启的情况下应用修改后的配置。如果设置为“用户”级别,就存在配置信息保存到appData文件夹下被清空缓存失效的风险。因此回过头来选择ini来保存这些配置信息。
5 g+ N8 `3 @0 `, s! Z2 i8 w- Uusing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;
namespace data_migration_desk.utils
{
// INI文件操作类
class IniFileHelper
{
string strIniFilePath; // ini配置文件路径
// 返回0表示失败,非0为成功
[DllImport("kernel32", CharSet = CharSet.Auto)]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
// 返回取得字符串缓冲区的长度
[DllImport("kernel32", CharSet = CharSet.Auto)]
private static extern long GetPrivateProfileString(string section, string key, string strDefault, StringBuilder retVal, int size, string filePath);
[DllImport("Kernel32.dll", CharSet = CharSet.Auto)]
public static extern int GetPrivateProfileInt(string section, string key, int nDefault, string filePath);
/// <summary>
/// 无参构造函数
/// </summary>
/// <returns></returns>
public IniFileHelper()
{
this.strIniFilePath = Directory.GetCurrentDirectory() + "\\Properties\\sysconfig.ini";
}
/// <summary>
/// 有参构造函数
/// </summary>
/// <param name="strIniFilePath">ini配置文件路径</param>
/// <returns></returns>
public IniFileHelper(string strIniFilePath)
{
if (strIniFilePath != null)
{
this.strIniFilePath = strIniFilePath;
}
}
/// <summary>
/// 获取ini配置文件中的字符串
/// </summary>
/// <param name="section">节名</param>
/// <param name="key">键名</param>
/// <param name="strDefault">默认值</param>
/// <param name="retVal">结果缓冲区</param>
/// <param name="size">结果缓冲区大小</param>
/// <returns>成功true,失败false</returns>
public bool GetIniString(string section, string key, string strDefault, StringBuilder retVal, int size)
{
long liRet = GetPrivateProfileString(section, key, strDefault, retVal, size, strIniFilePath);
return (liRet >= 1);
}
/// <summary>
/// 获取ini配置文件中的整型值
/// </summary>
/// <param name="section">节名</param>
/// <param name="key">键名</param>
/// <param name="nDefault">默认值</param>
/// <returns></returns>
public int GetIniInt(string section, string key, int nDefault)
{
return GetPrivateProfileInt(section, key, nDefault, strIniFilePath);
}
/// <summary>
/// 往ini配置文件写入字符串
/// </summary>
/// <param name="section">节名</param>
/// <param name="key">键名</param>
/// <param name="val">要写入的字符串</param>
/// <returns>成功true,失败false</returns>
public bool WriteIniString(string section, string key, string val)
{
long liRet = WritePrivateProfileString(section, key, val, strIniFilePath);
return (liRet != 0);
}
/// <summary>
/// 往ini配置文件写入整型数据
/// </summary>
/// <param name="section">节名</param>
/// <param name="key">键名</param>
/// <param name="val">要写入的数据</param>
/// <returns>成功true,失败false</returns>
public bool WriteIniInt(string section, string key, int val)
{
return WriteIniString(section, key, val.ToString());
}
}
} 读取:0 ^) v+ t n- d& U
IniFileHelper iniFileHelper = new IniFileHelper();
StringBuilder sb = new StringBuilder(60);
iniFileHelper.GetIniString("tokeninfo", "apiUrl", "", sb, sb.Capacity);
string aaa = sb.ToString(); 写入:6 n0 }9 G& ` J
IniFileHelper iniFileHelper = new IniFileHelper();
iniFileHelper.WriteIniString("tokeninfo", "apiUrl", "newString");
|