婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > asp.net 讀取配置文件方法

asp.net 讀取配置文件方法

熱門標簽:地圖標注多個行程 銅川小型外呼系統運營商 陜西人工外呼系統哪家好 廈門商鋪地圖標注 云南外呼電銷機器人系統 上海楊浦怎么申請申請400電話 海外地圖標注門市標 山西防封卡電銷卡套餐 浙江外呼系統怎么安裝
方法1:
復制代碼 代碼如下:

System.Collections.Specialized.NameValueCollection nvc = (System.Collections.Specialized.NameValueCollection)
System.Configuration.ConfigurationManager.GetSection(sectionName);

string keyValue = nvc.GetValues(keyName)[0].ToString();
方法2:
復制代碼 代碼如下:
System.Web.Configuration.WebConfigurationManager.AppSettings[keyName].ToString();

參考下面的文章

在C#中如何讀取配置文件
1. 配置文件概述:
應 用程序配置文件是標準的 XML 文件,XML 標記和屬性是區分大小寫的。它是可以按需要更改的,開發人員可以使用配置文件來更改設置,而不必重編譯應用程序。配置文件的根節點是 configuration。我們經常訪問的是appSettings,它是由.Net預定義配置節。我們經常使用的配置文件的架構是象下面的形式。先大 概有個印象,通過后面的實例會有一個比較清楚的認識。下面的“配置節”可以理解為進行配置一個XML的節點。
常見配置文件模式:
復制代碼 代碼如下:

configuration>
configSections> //配置節聲明區域,包含配置節和命名空間聲明
section> //配置節聲明
  sectionGroup> //定義配置節組
   section> //配置節組中的配置節聲明
appSettings> //預定義配置節
Custom element for configuration section> //配置節設置區域

2. 只有appSettings節的配置文件及訪問方法
下面是一個最常見的應用程序配置文件的例子,只有appSettings節。
復制代碼 代碼如下:

?xml version="1.0" encoding="utf-8"?>
configuration>
appSettings>
add key="connectionstring" value="User ID=sa;Data Source=.;Password=;Initial Catalog=test;Provider=SQLOLEDB.1;" />
add key="TemplatePATH" value="Template" />
/appSettings>
/configuration>

下面來看看這樣的配置文件如何方法。
string _connectionString=ConfigurationSettings.AppSettings["connectionstring"];
使用ConfigurationSettings類的靜態屬性AppSettings就可以直接方法配置文件中的配置信息。這個屬性的類型是NameValueCollection。
3. 自定義配置文件
3.1 自定義配置節
一個用戶自定義的配置節,在配置文件中分為兩部分:一是在configSections>/ configSections>配置節中聲明配置節(上面配置文件模式中的“section>”),另外是在 configSections>/ configSections >之后設置配置節(上面配置文件模式中的“Custom element for configuration section>”),有點類似一個變量先聲明,后使用一樣。聲明一個配置文件的語句如下:
section name=" " type=" "/>
section>:聲明新配置節,即可創建新配置節。
name:自定義配置節的名稱。
type:自定義配置節的類型,主要包括System.Configuration.SingleTagSectionHandler、 System.Configuration.DictionarySectionHandler、 System.Configuration.NameValueSectionHandler。
不同的type不但設置配置節的方式不一樣,最后訪問配置文件的操作上也有差異。下面我們就舉一個配置文件的例子,讓它包含這三個不同的type。
復制代碼 代碼如下:

?xml version="1.0" encoding="utf-8" ?>
configuration>
configSections>
section name="Test1" type="System.Configuration.SingleTagSectionHandler"/>
section name="Test2" type="System.Configuration.DictionarySectionHandler"/>
section name="Test3" type="System.Configuration.NameValueSectionHandler" />
/configSections>
Test1 setting1="Hello" setting2="World"/>
Test2>
add key="Hello" value="World" />
/Test2>
Test3>
add key="Hello" value="World" />
/Test3>
/configuration>

我們對上面的自定義配置節進行說明。在聲明部分使用section name="Test1" type="System.Configuration.SingleTagSectionHandler"/>聲明了一個配置節它的名字叫 Test1,類型為SingleTagSectionHandler。在設置配置節部分使用 Test1 setting1="Hello" setting2="World"/>設置了一個配置節,它的第一個設置的值是Hello,第二個值是World,當然還可以有更多。其它的兩個配 置節和這個類似。
下面我們看在程序中如何訪問這些自定義的配置節。我們用過ConfigurationSettings類的靜態方法GetConfig來獲取自定義配置節的信息。
public static object GetConfig(string sectionName);
下面是訪問這三個配置節的代碼:
復制代碼 代碼如下:

//訪問配置節Test1
IDictionary IDTest1 = (IDictionary)ConfigurationSettings.GetConfig("Test1");
string str = (string)IDTest1["setting1"] +" "+(string)IDTest1["setting2"];
MessageBox.Show(str); //輸出Hello World
//訪問配置節Test1的方法2
string[] values1=new string[IDTest1.Count];
IDTest1.Values.CopyTo(values1,0);
MessageBox.Show(values1[0]+" "+values1[1]); //輸出Hello World
//訪問配置節Test2
IDictionary IDTest2 = (IDictionary)ConfigurationSettings.GetConfig("Test2");
string[] keys=new string[IDTest2.Keys.Count];
string[] values=new string[IDTest2.Keys.Count];
IDTest2.Keys.CopyTo(keys,0);
IDTest2.Values.CopyTo(values,0);
MessageBox.Show(keys[0]+" "+values[0]);
//訪問配置節Test3
NameValueCollection nc=(NameValueCollection)ConfigurationSettings.GetConfig("Test3");
MessageBox.Show(nc.AllKeys[0].ToString()+" "+nc["Hello"]); //輸出Hello World

通過上面的代碼我們可以看出,不同的type通過GetConfig返回的類型不同,具體獲得配置內容的方式也不一樣。 配置節處理程序
返回類型
復制代碼 代碼如下:

SingleTagSectionHandler
Systems.Collections.IDictionary
DictionarySectionHandler
Systems.Collections.IDictionary
NameValueSectionHandler
Systems.Collections.Specialized.NameValueCollection

3.2 自定義配置節組
配置節組是使用sectionGroup>元素,將類似的配置節分到同一個組中。配置節組聲明 部分將創建配置節的包含元素,在configSections>元素中聲明配置節組,并將屬于該組的節置于 sectionGroup>元素中。下面是一個包含配置節組的配置文件的例子:
復制代碼 代碼如下:

?xml version="1.0" encoding="utf-8" ?>
configuration>
configSections>
sectionGroup name="TestGroup">
section name="Test" type="System.Configuration.NameValueSectionHandler"/>
/sectionGroup>
/configSections>
TestGroup>
Test>
add key="Hello" value="World"/>
/Test>
/TestGroup>
/configuration>

下面是訪問這個配置節組的代碼:
NameValueCollection nc=(NameValueCollection)ConfigurationSettings.GetConfig("TestGroup/Test");
MessageBox.Show(nc.AllKeys[0].ToString()+" "+nc["Hello"]); //輸出Hello World
C# 解析配置文件內容 System.Configuration
1. 創建配置節類
必須創建繼承自ConfigurationSection的對象才能進行配置數據讀寫操作,ConfigurationSection提供了索引器用來獲取和設置配置數據,需要注意的是擁有ConfigurationProperty特性的屬性才會被存儲,并且名稱要保持大小寫完全一致,如下面的代碼中,所有的"id"必須保持一樣。
復制代碼 代碼如下:

class ConfigSectionData : ConfigurationSection
{
[ConfigurationProperty("id")]
public int Id
{
get { return (int)this["id"]; }
set { this["id"] = value; }
}
[ConfigurationProperty("time")]
public DateTime Time
{
get { return (DateTime)this["time"]; }
set { this["time"] = value; }
}
}

2. 創建配置文件操作對象
復制代碼 代碼如下:

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ConfigSectionData data = new ConfigSectionData();
data.Id = 1000;
data.Time = DateTime.Now;
config.Sections.Add("add", data);
config.Save(ConfigurationSaveMode.Minimal);

上面的例子是操作 app.config,在根節點(configuration)下寫入名稱為"add"的配置數據。
復制代碼 代碼如下:

?xml version="1.0" encoding="utf-8" ?>
configuration>
configSections>
section name="add" type="ConsoleApplication1.ConfigSectionData, ... />
/configSections>
add id="1000" time="02/18/2006 21:51:06" />
/configuration>

需要注意的 VS2005 在IDE模式下會將信息寫入 *.vshost.exe.config,并且在程序關閉時覆寫該文件,因此您可能看不到您寫入的配置數據,只要在資源管理其中執行 *.exe 文件,您就可以在 *.exe.config 文件中看到結果了。
如果我們需要操作非缺省配置文件,可以使用ExeConfigurationFileMap對象。
復制代碼 代碼如下:

ExeConfigurationFileMap file = new ExeConfigurationFileMap();
file.ExeConfigFilename = "test.config";
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(file, ConfigurationUserLevel.None);
ConfigSectionData data = new ConfigSectionData();
data.Id = 1000;
data.Time = DateTime.Now;
config.Sections.Add("add", data);
config.Save(ConfigurationSaveMode.Minimal);

如果我們不希望在根節點下寫入配置數據,可以使用ConfigurationSectionGroup對象。
復制代碼 代碼如下:

ExeConfigurationFileMap file = new ExeConfigurationFileMap();
file.ExeConfigFilename = "test.config";
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(file, ConfigurationUserLevel.None);
ConfigSectionData data = new ConfigSectionData();
data.Id = 1000;
data.Time = DateTime.Now;
config.SectionGroups.Add("group1", new ConfigurationSectionGroup());
config.SectionGroups["group1"].Sections.Add("add", data);
config.Save(ConfigurationSaveMode.Minimal);

下面就是生成的配置文件。
復制代碼 代碼如下:

?xml version="1.0" encoding="utf-8"?>
configuration>
configSections>
sectionGroup name="group1" type="System.Configuration.ConfigurationSectionGroup, ... >
section name="add" type="ConsoleApplication1.ConfigSectionData, ... />
/sectionGroup>
/configSections>
group1>
add id="1000" time="02/18/2006 22:01:02" />
/group1>
/configuration>

3. 讀取配置文件
復制代碼 代碼如下:

ExeConfigurationFileMap file = new ExeConfigurationFileMap();
file.ExeConfigFilename = "test.config";
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(file, ConfigurationUserLevel.None);
ConfigSectionData data = config.SectionGroups["group1"].Sections["add"] as ConfigSectionData;
//ConfigSectionData data = config.Sections["add"] as ConfigSectionData; // 從根節讀取
if (data != null)
{
Console.WriteLine(data.Id);
Console.WriteLine(data.Time);
}

4. 寫配置文件
在寫入 ConfigurationSectionGroup 和 ConfigurationSection 前要判斷同名配置是否已經存在,否則會寫入失敗。
另外如果配置文件被其他Configuration對象修改,則保存會失敗,并拋出異常。建議采用Singleton模式。
復制代碼 代碼如下:

ExeConfigurationFileMap file = new ExeConfigurationFileMap();
file.ExeConfigFilename = "test.config";
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(file, ConfigurationUserLevel.None);
ConfigSectionData data = new ConfigSectionData();
data.Id = 2000;
data.Time = DateTime.Now;
ConfigurationSectionGroup group1 = config.SectionGroups["group1"];
if (group1 == null)
config.SectionGroups.Add("group1", new ConfigurationSectionGroup());
ConfigurationSection data = group1.Sections["add"] as config;
if (add == null)
config.SectionGroups["group1"].Sections.Add("add", data);
else
{
group1.Sections.Remove("add");
group1.Sections.Add("add", data);
// 或者直接修改原配置對象,前提是類型轉換要成功。
//ConfigSectionData configData = add as ConfigSectionData;
//configData.Id = data.Id;
//configData.Time = data.Time;
}
config.Save(ConfigurationSaveMode.Minimal);

5. 刪除配置節
復制代碼 代碼如下:

刪除ConfigurationSectionGroup
config.SectionGroups.Remove("group1");
//config.SectionGroups.Clear();
config.Save(ConfigurationSaveMode.Minimal);
刪除ConfigurationSection
config.Sections.Remove("add1");
//config.Sections.Clear();
if (config.SectionGroups["group1"] != null)
{
config.SectionGroups["group1"].Sections.Remove("add2");
//config.SectionGroups["group1"].Sections.Clear();
}
config.Save(ConfigurationSaveMode.Minimal);

6. 其他
可以使用 ConfigurationManager.OpenMachineConfiguration() 來操作 Machine.config 文件。
或者使用 System.Web.Configuration 名字空間中的 WebConfigurationManager 類來操作 ASP.net 配置文件。
ConfigurationManager還提供了AppSettings、ConnectionStrings、GetSection()等便捷操作。
7. 使用自定義類
可以使用自定義類,不過需要定義一個轉換器。
復制代碼 代碼如下:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Globalization;
using System.ComponentModel;
// 要寫入配置文件的自定義類
class CustomData
{
public CustomData(string s)
{
this.s = s;
}
private string s;
public string S
{
get { return s; }
set { s = value; }
}
}
// 自定義的轉換器(演示代碼省略了類型判斷)
class CustomConvert : ConfigurationConverterBase
{
public override bool CanConvertFrom(ITypeDescriptorContext ctx, Type type)
{
return (type == typeof(string));
}
public override object ConvertTo(ITypeDescriptorContext ctx, CultureInfo ci, object value, Type type)
{
return (value as CustomData).S;
}
public override object ConvertFrom(ITypeDescriptorContext ctx, CultureInfo ci, object data)
{
return new CustomData((string)data);;
}
}
class ConfigSectionData : ConfigurationSection
{
[ConfigurationProperty("id")]
public int Id
{
get { return (int)this["id"]; }
set { this["id"] = value; }
}
[ConfigurationProperty("time")]
public DateTime Time
{
get { return (DateTime)this["time"]; }
set { this["time"] = value; }
}
[ConfigurationProperty("custom")]
[TypeConverter(typeof(CustomConvert))] // 指定轉換器
public CustomData Custom
{
get { return (CustomData)this["custom"]; }
set { this["custom"] = value; }
}
}
public class Program
{
static void Main(string[] args)
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ConfigSectionData data = new ConfigSectionData();
data.Id = 1000;
data.Time = DateTime.Now;
data.Custom = new CustomData("abcdefg...");
config.Sections.Add("add", data);
config.Save(ConfigurationSaveMode.Minimal);
// 讀取測試
ConfigSectionData configData = (ConfigSectionData)config.Sections["add"];
Console.WriteLine(configData.Custom.S);
}
}

保存后的配置文件
復制代碼 代碼如下:

?xml version="1.0" encoding="utf-8" ?>
configuration>
configSections>
section name="add" type="..." />
/configSections>
add id="1000" time="04/17/2006 22:06:58" custom="abcdefg..." />
/configuration>

更詳細的信息可以看 MSDN 中關于 System.Configuration.ConfigurationConverterBase 的說明。
您可能感興趣的文章:
  • ASP.Net動態讀取Excel文件最簡方法
  • 如何在ASP.NET Core類庫項目中讀取配置文件詳解
  • asp.net讀取模版并寫入文本文件
  • ASP.NET對txt文件相關操作(讀取、寫入、保存)
  • ASP.NET中上傳并讀取Excel文件數據示例
  • asp.net讀取excel文件的三種方法示例
  • asp.net讀取磁盤文件、刪除實例代碼
  • ASP.NET(C#)讀取Excel的文件內容
  • C#/.NET讀取或修改文件的創建時間及修改時間詳解

標簽:常州 西雙版納 信陽 孝感 萊蕪 朔州 自貢 許昌

巨人網絡通訊聲明:本文標題《asp.net 讀取配置文件方法》,本文關鍵詞  asp.net,讀取,配置文件,方法,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《asp.net 讀取配置文件方法》相關的同類信息!
  • 本頁收集關于asp.net 讀取配置文件方法的相關信息資訊供網民參考!
  • 推薦文章
    婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av
    大胆欧美人体老妇| 国产日韩欧美精品在线| 精品国产123| www.久久久久久久久| 三级一区在线视频先锋| 亚洲精品国产精品乱码不99| 欧美国产日本韩| 欧美在线高清视频| 色综合天天做天天爱| 一本高清dvd不卡在线观看| 亚洲一卡二卡三卡四卡无卡久久 | 精品国产伦一区二区三区免费 | 欧美精品一区二区三| 男女男精品网站| 国内久久精品视频| 亚洲免费av网站| 亚洲午夜免费电影| 精品久久久久久久久久久久久久久久久 | 欧美亚州韩日在线看免费版国语版| 中文字幕精品在线不卡| 色综合一区二区| 国产性色一区二区| 精品女同一区二区| 日本一区二区三区dvd视频在线| 777欧美精品| 色哟哟一区二区三区| 精品国产欧美一区二区| 欧美视频一区二区三区四区| 国产精品综合在线视频| 美女国产一区二区| 国产福利一区二区三区在线视频| 亚洲夂夂婷婷色拍ww47| 欧美制服丝袜第一页| 九九国产精品视频| 国模一区二区三区白浆| 精品国产第一区二区三区观看体验 | 久久成人综合网| 国产精品99精品久久免费| 久久婷婷国产综合精品青草| 欧美怡红院视频| 欧美一区二区私人影院日本| 欧美三级蜜桃2在线观看| 精品无码三级在线观看视频 | 日韩精品专区在线影院重磅| 91精品国产综合久久久久| 欧美一区二区精品在线| 国产天堂亚洲国产碰碰| 日韩欧美视频在线| 欧美性生活影院| 成人sese在线| 国产日本欧洲亚洲| 国产精品一区二区黑丝| 色视频成人在线观看免| 欧美日韩国产成人在线免费| 亚洲精品美腿丝袜| 国产一二精品视频| 久久久精品tv| 日本女人一区二区三区| 色婷婷精品大视频在线蜜桃视频| 亚洲人成精品久久久久久| 国产精品夜夜嗨| 久久久久久久免费视频了| 日韩高清不卡一区二区| 一区二区三区不卡视频 | 亚洲国产精品天堂| 久久久国产精华| 国产成人精品免费在线| 欧美精品久久天天躁| 日本sm残虐另类| 欧美丝袜丝nylons| 日韩午夜中文字幕| 激情综合色播五月| 欧美一区二区三区婷婷月色 | 韩国精品主播一区二区在线观看| 欧美老肥妇做.爰bbww| 日本中文字幕一区二区有限公司| 欧美做爰猛烈大尺度电影无法无天| 精品久久久久久亚洲综合网| 欧美在线免费播放| 欧美日韩在线一区二区| 蜜桃传媒麻豆第一区在线观看| 色老汉一区二区三区| 91精选在线观看| 天堂资源在线中文精品| 国产成人在线视频免费播放| 日韩美女视频19| 91在线观看高清| 天堂资源在线中文精品| 日韩欧美卡一卡二| 久久99精品国产.久久久久久| 337p粉嫩大胆色噜噜噜噜亚洲| 日韩欧美亚洲国产精品字幕久久久| 欧美日韩一级黄| 欧美久久久久中文字幕| 欧美日韩中字一区| av在线不卡电影| 成人avav影音| 欧美伦理影视网| 免费高清视频精品| 麻豆国产精品视频| 欧美大片顶级少妇| 亚洲成人精品一区二区| 日韩一区二区三区视频在线| 麻豆成人综合网| 亚洲婷婷综合色高清在线| 色八戒一区二区三区| 调教+趴+乳夹+国产+精品| 国产精品欧美综合在线| 91看片淫黄大片一级在线观看| 久久不见久久见免费视频1| 久久精品亚洲精品国产欧美 | 蜜臀va亚洲va欧美va天堂| 欧美精品一区二区在线观看| 99国产精品视频免费观看| 欧美体内she精高潮| 26uuu色噜噜精品一区二区| 国产精品福利一区二区| 国产毛片精品一区| 精品污污网站免费看| 国产精品久久久久影院色老大| 国内精品久久久久影院薰衣草| 欧美日韩在线观看一区二区| 亚洲国产色一区| 国产日本欧洲亚洲| 日韩亚洲欧美一区二区三区| 国产成人福利片| 国产在线国偷精品免费看| 亚洲精品久久嫩草网站秘色| 欧美在线播放高清精品| 678五月天丁香亚洲综合网| 亚洲欧美日韩中文播放| k8久久久一区二区三区| 国产精品成人免费在线| 成人中文字幕在线| 久久综合久色欧美综合狠狠| 国产伦理精品不卡| 精品精品国产高清a毛片牛牛| 麻豆精品一区二区三区| 26uuu亚洲综合色欧美 | 国产精品久久久久影视| 久久99久久99精品免视看婷婷| 欧美一区二区三区免费视频| 久久亚洲精华国产精华液 | 亚洲精品国产成人久久av盗摄| 风间由美一区二区av101| 亚洲男同性视频| 欧美精品xxxxbbbb| 高清在线不卡av| 日韩影视精彩在线| 99精品1区2区| 日韩精品一区二区三区视频在线观看| 夜夜夜精品看看| 中文字幕av资源一区| 国产日产欧美一区二区视频| 欧美成人在线直播| 欧美日韩免费观看一区三区| 91原创在线视频| 亚洲私人黄色宅男| 欧美日韩在线不卡| av电影一区二区| 久久99国产精品尤物| 亚洲一区二区av在线| 国产精品欧美极品| 国产日韩欧美一区二区三区乱码| 欧美日韩国产免费一区二区 | 91啪在线观看| 懂色av中文一区二区三区| 一区二区三区精品在线观看| 久久精品国产秦先生| 亚洲欧美另类久久久精品| 久久久久久电影| 国产69精品久久久久777| 亚洲国产精品久久久久秋霞影院| 精品精品国产高清a毛片牛牛| 亚洲综合图片区| 欧美性受极品xxxx喷水| 不卡的av在线播放| 成人av在线看| 91一区二区在线| 精品国产乱子伦一区| 久久久国产综合精品女国产盗摄| 国产欧美日韩久久| 精品一区二区日韩| 国产成人av一区二区三区在线| 国产一区二三区好的| www.久久精品| 一区二区三区 在线观看视频 | 国产欧美日韩中文久久| 国产精品久久久久久久久免费丝袜 | 久久精品国产亚洲aⅴ| 美女视频黄久久| 国产91丝袜在线观看| 丰满少妇久久久久久久| 成人激情小说乱人伦| 91麻豆精品国产91久久久更新时间| 亚洲国产欧美一区二区三区丁香婷| 美美哒免费高清在线观看视频一区二区| 免费成人性网站| 久久国产生活片100| 久久99在线观看|