[Web.config]

<appSettings>
  <add key="Test" value="S901,ChkDele,BtnAddNew,BtnSave,BtnDelete"/>
  <add key="FunctionURL" value=" Defaultpage,default.aspx;TreeView,TreeViewpage.aspx;Testpage,Test.aspx"/>
</appSettings>

 ※使用寫入的動作時,Web.Config檔一定要將安全性的Users的權限設定為允許修改。

 

寫一個 Function 去抓 AppSetting 的值

using System;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web.Configuration;

 

    public string GetFunctionURL(string FunctionName)
    {
        
        string FName = FunctionName;
        string URLstr = GetAppSetting("FunctionURL");
        string[] URLs = URLstr.Split(';');
        string FURL = "";
       
        foreach (string FunctionURL in URLs)
        {
            string[] str = FunctionURL.Split(',');
            string Name = str[0];
            string URL = str[1];

            if (FName == Name)
            {
                FURL = URL;
            }
        }

        return FURL.ToString();
    }

 

//************** 重點是這一支 ***************** 

    public string GetAppSetting(string appSettingName)
    {
        string appsettingName = appSettingName;

        if (!string.IsNullOrEmpty(appSettingName))
        {           
            NameValueCollection appSettings = WebConfigurationManager.AppSettings as NameValueCollection;
            System.Collections.IEnumerator appSettingsEnum = appSettings.GetEnumerator();
            int i = 0;
            while (appSettingsEnum.MoveNext())
            {
                string key = appSettings.AllKeys[i].ToString();
                string value = appSettings[key];
                if (key.Equals(appsettingName))
                {
                    appsettingName = value;
                    break;
                }
                i++;
            }
        }
        return appsettingName;
    }

 

--

參考文章:http://www.dotblogs.com.tw/joumingt/archive/2009/06/01/8631.aspx

創作者介紹
創作者 ⒶⓂⓎ 的頭像
ⒶⓂⓎ

ㄚ咪的幸福收納盒

ⒶⓂⓎ 發表在 痞客邦 留言(0) 人氣()