110,545
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;
namespace WindowsApplication1
{
class Config
{
private Config() { }
private static Config instance = new Config();
internal static Config Instance
{
get { return Config.instance; }
set { Config.instance = value; }
}
//写配置文件
[DllImport("kernel32")]
private static extern bool WritePrivateProfileString(
string section,//段落
string key,//小节点
string val,//小节点的值
string filePath);//配置文件的路径
//读配置文件
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(
string section,//段落
string key,//小节点
string def,//读取失败时候的缺省值
StringBuilder retVal,//读取的值
int size,//读取值的大小
string filePath);//配置文件的路径
/// <summary>
/// 创建新的配置项
/// </summary>
/// <param name="rootkey">段落</param>
/// <param name="subkey">子节点</param>
/// <param name="keyvalue">字节点的值</param>
/// <returns>如果返回空则操作成功否则操作失败</returns>
public string CreateNewConfig(string rootkey, string subkey, string keyvalue)
{
if (!File.Exists(Form1.ConfigFile))
File.Create(Form1.ConfigFile).Close();
if (!WritePrivateProfileString(rootkey, subkey, keyvalue, Form1.ConfigFile))
return "创建段落失败!";
return string.Empty;
}
/// <summary>
/// 更新指定节点的值
/// </summary>
/// <param name="rootkey">段落</param>
/// <param name="subkey">子节点</param>
/// <param name="keyvalue">字节点的值</param>
/// <returns>如果返回空则操作成功否则操作失败</returns>
public string UpdateConfig(string rootkey, string subkey, string keyvalue)
{
if (!File.Exists(Form1.ConfigFile))
File.Create(Form1.ConfigFile).Close();
if (!WritePrivateProfileString(rootkey, subkey, keyvalue, Form1.ConfigFile))
return "修改关键值失败!";
return string.Empty;
}
/// <summary>
/// 读取指定节点的值
/// </summary>
/// <param name="rootkey">段落</param>
/// <param name="subkey">子节点</param>
/// <param name="keyvalue">字节点的值</param>
/// <returns>如果返回空则操作成功否则操作失败</returns>
public string ReadConfig(string rootkey, string subkey)
{
if (!File.Exists(Form1.ConfigFile))
throw new Exception("没有找到配置文件!");
//File.Create(ConfigFile).Close();
StringBuilder RetVal = new StringBuilder();
int readLe = GetPrivateProfileString(rootkey, subkey, "读取配置信息失败!", RetVal, 1024, Form1.ConfigFile);
if (readLe != RetVal.Length)
return "Error";
return RetVal.ToString();
}
}
}
string connectString = ConfigurationManager.ConnectionStrings["AName"].ConnectionString;
string connectString = ConfigurationManager.ConnectionStrings["BName"].ConnectionString;
读取app.config中的数据
App.config中格式如下:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="UserInfo" type="System.Configuration.AppSettingsSection"/>
<section name="directoryInfo" type="System.Configuration.AppSettingsSection"/>
</configSections>
<UserInfo>
<add key="DVRUSR" value="" />
<add key="DVRPWD" value="" />
</UserInfo>
<directoryInfo>
<add key="defaultPath" value="22" />
<add key="savePath" value="11" />
</directoryInfo>
</configuration>
读取方式如下:
string a = GetSettingValue("directoryInfo", "savePath");
///////////////////////////////////////////////////////////////////////////////
private string m_configFile = System.Reflection.Assembly.GetExecutingAssembly().CodeBase.Substring(8);
private string GetSettingValue(string argSectionName, string argKey)
{
try
{
Return GetReadOnlySetting(m_configFile, argSectionName, argKey);
}
catch (Exception ex)
{
WriteLogInfo.WriteLog(LogLevel.LV_ERROR, ex.ToString());
return String.Empty;
}
}
///////////////////////////////////////////////////////////////////////////////
private static readonly string INIFILE = ".ini";
private static Hashtable m_ReadOnlySetting = new Hashtable();
public static string GetReadOnlySetting(string argFileName, string argSection, string argKey)
{
-------------------------------判断app.config文件是否存在----------------------
if (m_ReadOnlySetting[argFileName] == null)
{
if (m_ReadOnlySetting[argFileName] == null)
{
Hashtable htFile = new Hashtable();
m_ReadOnlySetting.Add(argFileName, htFile);
}
}
Hashtable file = m_ReadOnlySetting[argFileName] as Hashtable;
-------------------------------判断节点是否存在------------------------------
if (file[argSection] == null)
{
if (file[argSection] == null)
{
KeyValue keyValue = new KeyValue();
Hashtable htSection;
if (argFileName.EndsWith(INIFILE))
{
htSection = keyValue.GetIniSection(argFileName, argSection);
}
else
{
htSection = keyValue.GetValueList(argFileName, argSection);
}
file.Add(argSection, htSection);
}
}
Hashtable section = file[argSection] as Hashtable;
-------------------------------判断节点是否有值-----------------------------
string strValue = section[argKey] as string;
if (strValue == null || strValue == String.Empty)
{
throw new ArgumentNullException(String.Format("{0}的设定文件" +"<configuration><{1}><{2}>的构成不存在", argFileName, argSection, argKey));
}
return strValue;
}
修改方式如下:
SetSettingValue(m_configFile, "directoryInfo", "savePath", _folderPath);
///////////////////////////////////////////////////////////////////////////////
public static void SetSettingValueHandle(string argFileName, string argSection, string argKey, string argValue)
{
try
{
Configuration config = ConfigurationManager.OpenExeConfiguration(argFileName);
ConfigurationSection configSection = config.Sections[argSection];
if (configSection != null)
{
AppSettingsSection appSettingsSection = configSection as AppSettingsSection;
if (appSettingsSection != null && appSettingsSection.Settings.Count > 0)
{
appSettingsSection.Settings[argKey].Value = argValue;
}
}
config.Save();
}
catch (Exception ex)
{
throw new ArgumentNullException(String.Format("{0}的设定文件" +"<configuration><{1}><{2}>的构成不存在", argFileName, argSection, argKey), ex);
}
}