簡體   English   中英

從web.config讀取並格式化字符串

[英]reading from web.config and format a string

我排除了一些從我的應用程序發送郵件的ipaddress。在這里我在web.config文件中創建了自定義標簽

<configSections>
  <sectionGroup name="ExcludeIPAddressListSectionGroup">
    <section name="IPAddressListSection" type="CustomConfigurationHandler.CustomConfiguration, CustomConfigurationHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" allowLocation="true" allowDefinition="Everywhere"/>
  </sectionGroup>
</configSections>

<ExcludeIPAddressListSectionGroup>
  <IPAddressListSection>
      <ExcludedList1 Range="StartingIP=192.168.185.1; EndingIP=192.168.185.50" ></ExcludedList1>
      <ExcludedList2 Range="StartingIP=192.168.185.51;EndingIP=192.168.185.51" ></ExcludedList2>
  </IPAddressListSection>
</ExcludeIPAddressListSectionGroup>

我創建了ExcludeIPAddressListSectionGroup部分,並給出了范圍以排除那些ips

正在像這樣從web.config中讀取自定義標簽

 Hashtable config = (Hashtable)ConfigurationManager.GetSection("ExcludeIPAddressListSectionGroup/IPAddressListSection");
 foreach (DictionaryEntry deKey in config)
 {
     Hashtable attribs = (Hashtable)deKey.Value;
     foreach (DictionaryEntry deAttrib in attribs)
     {
         string range = deAttrib.Value.ToString();
         string[] range_arr = range.Split(';');

         foreach (string range_str in range_arr)
         {
             Response.Write(range_str+"<br>");
         }
     }
 }

我需要格式化字符串,然后將startip和end ip傳遞給此函數bool Between(長值,左長,右長)

使用以下代碼。 它適用於IPV4。

        List<string> lstIPValues = new List<string>();
        foreach (string range_str in range_arr)
        {
             //again split to get ragne value into List of string.
            string[] ipValues = range_str.Split('=');
            lstIPValues.Add(ipValues[1]);    
        }
        Between(valueToBeChecked,, BitConverter.ToInt64(IPAddress.Parse(lstIPValues[0]).GetAddressBytes(), 0), BitConverter.ToInt64(IPAddress.Parse(lstIPValues[1]).GetAddressBytes(), 0));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM