LOGO OA教程 ERP教程 模切知識(shí)交流 PMS教程 CRM教程 開(kāi)發(fā)文檔 其他文檔  
 
網(wǎng)站管理員

C#讀取或設(shè)置配置文件Web.config/*.exe.config中節(jié)點(diǎn)數(shù)據(jù)的輔助類(lèi)

admin
2021年3月3日 16:40 本文熱度 4368
using System.Xml;
using System.IO;
using System;

namespace Framework.Common
{
/// <summary>
/// 用于獲取或設(shè)置Web.config/*.exe.config中節(jié)點(diǎn)數(shù)據(jù)的輔助類(lèi)
/// </summary>
public sealed class AppConfig
{
private string filePath;

/// <summary>
/// 從當(dāng)前目錄中按順序檢索Web.Config和*.App.Config文件。
/// 如果找到一個(gè),則使用它作為配置文件;否則會(huì)拋出一個(gè)ArgumentNullException異常。
/// </summary>
public AppConfig()
{
string webconfig = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Web.Config");
string appConfig = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile.Replace(".vshost", "");

if (File.Exists(webconfig))
{
filePath = webconfig;
}
else if (File.Exists(appConfig))
{
filePath = appConfig;
}
else
{
throw new ArgumentNullException("沒(méi)有找到Web.Config文件或者應(yīng)用程序配置文件, 請(qǐng)指定配置文件");
}
}

/// <summary>
/// 用戶(hù)指定具體的配置文件路徑
/// </summary>
/// <param name="configFilePath">配置文件路徑(絕對(duì)路徑)
public AppConfig(string configFilePath)
{
filePath = configFilePath;
}

/// <summary>
/// 設(shè)置程序的config文件
/// </summary>
/// <param name="keyName">鍵名
/// <param name="keyValue">鍵值
public void AppConfigSet(string keyName, string keyValue)
{
//由于存在多個(gè)Add鍵值,使得訪問(wèn)appSetting的操作不成功,故注釋下面語(yǔ)句,改用新的方式
/*
string xpath = "http://add[@key=''" + keyName + "'']";
XmlDocument document = new XmlDocument();
document.Load(filePath);

XmlNode node = document.SelectSingleNode(xpath);
node.Attributes["value"].Value = keyValue;
document.Save(filePath);
*/

XmlDocument document = new XmlDocument();
document.Load(filePath);

XmlNodeList nodes = document.GetElementsByTagName("add");
for (int i = 0; i < nodes.Count; i++)
{
//獲得將當(dāng)前元素的key屬性
XmlAttribute attribute = nodes[i].Attributes["key"];
//根據(jù)元素的第一個(gè)屬性來(lái)判斷當(dāng)前的元素是不是目標(biāo)元素
if (attribute != null && (attribute.Value == keyName))
{
attribute = nodes[i].Attributes["value"];
//對(duì)目標(biāo)元素中的第二個(gè)屬性賦值
if (attribute != null)
{
attribute.Value = keyValue;
break;
}
}
}
document.Save(filePath);
}

/// <summary>
/// 讀取程序的config文件的鍵值。
/// 如果鍵名不存在,返回空
/// </summary>
/// <param name="keyName">鍵名
/// <returns></returns>
public string AppConfigGet(string keyName)
{
string strReturn = string.Empty;
try
{
XmlDocument document = new XmlDocument();
document.Load(filePath);

XmlNodeList nodes = document.GetElementsByTagName("add");
for (int i = 0; i < nodes.Count; i++)
{
//獲得將當(dāng)前元素的key屬性
XmlAttribute attribute = nodes[i].Attributes["key"];
//根據(jù)元素的第一個(gè)屬性來(lái)判斷當(dāng)前的元素是不是目標(biāo)元素
if (attribute != null && (attribute.Value == keyName))
{
attribute = nodes[i].Attributes["value"];
if (attribute != null)
{
strReturn = attribute.Value;
break;
}
}
}
}
catch
{
;
}

return strReturn;
}

/// <summary>
/// 獲取指定鍵名中的子項(xiàng)的值
/// </summary>
/// <param name="keyName">鍵名
/// <param name="subKeyName">以分號(hào)(;)為分隔符的子項(xiàng)名稱(chēng)
/// <returns>對(duì)應(yīng)子項(xiàng)名稱(chēng)的值(即是=號(hào)后面的值)</returns>
public string GetSubValue(string keyName, string subKeyName)
{
string connectionString = AppConfigGet(keyName).ToLower();
string[] item = connectionString.Split(new char[] { '';'' });

for (int i = 0; i < item.Length; i++)
{
string itemValue = item[i].ToLower();
if (itemValue.IndexOf(subKeyName.ToLower()) >= 0) //如果含有指定的關(guān)鍵字
{
int startIndex = item[i].IndexOf("="); //等號(hào)開(kāi)始的位置
return item[i].Substring(startIndex + 1); //獲取等號(hào)后面的值即為Value
}
}
return string.Empty;
}

#region 一些常用的配置項(xiàng)屬性

/// <summary>
/// 從配置文件獲取權(quán)限系統(tǒng)鏈接(配置項(xiàng)HWSecurity的值)
/// </summary>
public string HWSecurity
{
get
{
return AppConfigGet("HWSecurity");
}
}

/// <summary>
/// 系統(tǒng)的標(biāo)識(shí)ID(配置項(xiàng)System_ID的值)
/// </summary>
public string System_ID
{
get
{
return AppConfigGet("System_ID");
}
}

/// <summary>
/// 應(yīng)用程序名稱(chēng)(配置項(xiàng)ApplicationName的值)
/// </summary>
public string AppName
{
get
{
return AppConfigGet("ApplicationName");
}
}

/// <summary>
/// 軟件廠商名稱(chēng)(配置項(xiàng)Manufacturer的值)
/// </summary>
public string Manufacturer
{
get
{
return AppConfigGet("Manufacturer");
}
}

/// <summary>
/// 設(shè)置程序的config文件的Enterprise Library的數(shù)據(jù)庫(kù)鏈接地址
/// </summary>
/// <param name="keyName">鍵名
/// <param name="keyValue">鍵值
public void SetConnectionString(string keyName, string keyValue)
{
XmlDocument document = new XmlDocument();
document.Load(filePath);

XmlNodeList nodes = document.GetElementsByTagName("add");
for (int i = 0; i < nodes.Count; i++)
{
//獲得將當(dāng)前元素的name屬性
XmlAttribute att = nodes[i].Attributes["name"];
//根據(jù)元素的第一個(gè)屬性來(lái)判斷當(dāng)前的元素是不是目標(biāo)元素
if (att != null && (att.Value == keyName))
{
att = nodes[i].Attributes["connectionString"];
if (att != null)
{
att.Value = keyValue;
break;
}
}
}
document.Save(filePath);
}

/// <summary>
/// 讀取程序的config文件Enterprise Library的數(shù)據(jù)庫(kù)鏈接地址
/// </summary>
/// <param name="keyName">鍵名
/// <returns></returns>
public string GetConnectionString(string keyName)
{
string strReturn = string.Empty;
try
{
XmlDocument document = new XmlDocument();
document.Load(filePath);

XmlNodeList nodes = document.GetElementsByTagName("add");
for (int i = 0; i < nodes.Count; i++)
{
//獲得將當(dāng)前元素的key屬性
XmlAttribute att = nodes[i].Attributes["name"];
//根據(jù)元素的第一個(gè)屬性來(lái)判斷當(dāng)前的元素是不是目標(biāo)元素
if (att != null && (att.Value == keyName))
{
att = nodes[i].Attributes["connectionString"];
if (att != null)
{
strReturn = att.Value;
break;
}
}
}
}
catch
{ ; }

return strReturn;
}

/// <summary>
/// 獲取數(shù)據(jù)庫(kù)配置信息
/// </summary>
/// <param name="keyName">節(jié)點(diǎn)名稱(chēng)
/// <returns></returns>
public DatabaseInfo GetDatabaseInfo(string keyName)
{
string connectionString = GetConnectionString(keyName);
return new DatabaseInfo(connectionString);
}

/// <summary>
/// 設(shè)置數(shù)據(jù)庫(kù)配置信息
/// </summary>
/// <param name="keyName">
/// <param name="databaseInfo">
public void SetDatabaseInfo(string keyName, DatabaseInfo databaseInfo)
{
SetConnectionString(keyName, databaseInfo.ConnectionString);
}

#endregion
}

}

該文章在 2021/3/3 16:40:31 編輯過(guò)
關(guān)鍵字查詢(xún)
相關(guān)文章
正在查詢(xún)...
點(diǎn)晴ERP是一款針對(duì)中小制造業(yè)的專(zhuān)業(yè)生產(chǎn)管理軟件系統(tǒng),系統(tǒng)成熟度和易用性得到了國(guó)內(nèi)大量中小企業(yè)的青睞。
點(diǎn)晴PMS碼頭管理系統(tǒng)主要針對(duì)港口碼頭集裝箱與散貨日常運(yùn)作、調(diào)度、堆場(chǎng)、車(chē)隊(duì)、財(cái)務(wù)費(fèi)用、相關(guān)報(bào)表等業(yè)務(wù)管理,結(jié)合碼頭的業(yè)務(wù)特點(diǎn),圍繞調(diào)度、堆場(chǎng)作業(yè)而開(kāi)發(fā)的。集技術(shù)的先進(jìn)性、管理的有效性于一體,是物流碼頭及其他港口類(lèi)企業(yè)的高效ERP管理信息系統(tǒng)。
點(diǎn)晴WMS倉(cāng)儲(chǔ)管理系統(tǒng)提供了貨物產(chǎn)品管理,銷(xiāo)售管理,采購(gòu)管理,倉(cāng)儲(chǔ)管理,倉(cāng)庫(kù)管理,保質(zhì)期管理,貨位管理,庫(kù)位管理,生產(chǎn)管理,WMS管理系統(tǒng),標(biāo)簽打印,條形碼,二維碼管理,批號(hào)管理軟件。
點(diǎn)晴免費(fèi)OA是一款軟件和通用服務(wù)都免費(fèi),不限功能、不限時(shí)間、不限用戶(hù)的免費(fèi)OA協(xié)同辦公管理系統(tǒng)。
Copyright 2010-2025 ClickSun All Rights Reserved

黄频国产免费高清视频,久久不卡精品中文字幕一区,激情五月天AV电影在线观看,欧美国产韩国日本一区二区
亚洲无aV码在线中文字幕 | 午夜福利在线观看爽爽爽 | 偷自视频区在线 | 亚洲精品高清国产一线久久 | 亚洲综合久久精品网 | 天天摸天天碰天天添中文 |