请问在ASP.NET中如何用自己的配置文件,不保存配置信息到web.config里。

解决方案 »

  1.   


    if you wan to use your custome config settings 
    you can inherit the IConfigurationSectionHandler if your .net version is 1.x
    you can inherit the ConfigurationSection if your .net version is 2.0if you want to store you config settings in another file,wait a second,I will write a demo for you.
      

  2.   

    通过XML进行配置,操作XML
      

  3.   

    当然可以使用任何文本文件进行存储配置信息
    比如:config.cs
    domain:abc.com
    admin:zhnagsan
    password:123456
    .....
    无非就是涉及对文件的操作而已
    (使用保护文件安全性会高一些)
      

  4.   

    if you wan to use your custome config settings 
    you can inherit the IConfigurationSectionHandler if your .net version is 1.x 
    you can inherit the ConfigurationSection if your .net version is 2.0 if you want to store you config settings in another file,wait a second,I will write a demo for youUPDATE: 2009-03-29you can download here:http://download.csdn.net/source/1154349code snippet:an config file like this one:<?xml version="1.0"?>
    <websiteConfig>
      <siteUrl><![CDATA[http://huobazi.aspxboy.com/]]></siteUrl>
      <themeConfig defaultTheme="Red">
        <theme name="Blue" />
        <theme name="Red" />
        <theme name="Black" />
      </themeConfig>
    </websiteConfig>
    we want to store it in our own config file , NOT web.config.
    wen can use a serializable class :
    namespace Huobaz.XiaHouWeni.CSDN.TestWebapplication
    {
        [XmlRoot( "websiteConfig" )]//serializable attribute
        public class WebsiteConfiguration
        {
            [XmlElement("siteUrl")]//serializable attribute
            public string SiteUrl { get; set; }        [XmlElement( "themeConfig" )]//serializable attribute
            public ThemeConfig ThemeConfig { get; set; }
        }
    }
      

  5.   

    other classes:public class ThemeConfig
        {
            [XmlAttribute( "defaultTheme" )]//serializable attribute
            public string DefaultThemeName { get; set; }        [XmlElement( "theme" )]//serializable attribute
            public ThemeCollection Themes { get; set; }        private Theme DefaultTheme
            {
                get { return Themes[DefaultThemeName]; }
            }    }    public class Theme
        {
            [XmlAttribute( "name" )]//serializable attribute
            public string Name { get; set; }
        }    public class ThemeCollection : KeyedCollection<string, Theme>
        {
            protected override string GetKeyForItem(Theme item)
            {
                return item.Name;
            }
        }then we write a helper class :internal sealed class ConfigLoader
        {
            private ConfigLoader() { }        public static T LoadConfig<T>() where T : class
            {
                return LoadConfig<T>( null );
            }        public static T LoadConfig<T>(string fileName) where T : class
            {
                if ( string.IsNullOrEmpty( fileName ) )
                {
                    fileName = HttpContext.Current.Server.MapPath( string.Concat( "~/", typeof( T ).Name ,".config") );
                }            string cacheKey = fileName;            T configObj =  HttpRuntime.Cache[cacheKey] as T;
                if ( configObj == null )
                {
                    configObj = LoadFromXml<T>( fileName );
                    HttpRuntime.Cache.Insert( cacheKey, configObj, new System.Web.Caching.CacheDependency( fileName ) );
                }            return configObj;
            }        private static T LoadFromXml<T>(string fileName) where T : class
            {
                FileStream fs = null;
                try
                {
                    XmlSerializer serializer = new XmlSerializer( typeof( T ) );
                    fs = new FileStream( fileName, FileMode.Open, FileAccess.Read );
                    return (T)serializer.Deserialize( fs );
                }
                catch ( Exception ex )
                {
                    return null;
                }
                finally
                {
                    if ( fs != null )
                    {
                        fs.Close( );
                    }
                }
            }
      

  6.   

    then we use a manager class    public static class ConfigManager
        {
            public static WebsiteConfiguration WebSitConfig
            {
                get
                {
                    return ConfigLoader.LoadConfig<WebsiteConfiguration>( );
                }
            }
        }
    HOW TO USE            this.txtSiteUrl.Text = ConfigManager.WebSitConfig.SiteUrl;
                this.txtCurrentThemeName.Text = ConfigManager.WebSitConfig.ThemeConfig.DefaultThemeName;            this.themeGrid.DataSource = ConfigManager.WebSitConfig.ThemeConfig.Themes;
                this.themeGrid.DataBind( );
    we use some serializable class to serialize the XML file,and push it to ASP.NET cache
    when the file was modified the class instance will be changed .you can download here:http://download.csdn.net/source/1154349
    and run it
      

  7.   

    自己的配置文件?简单点的为什么不直接用Profile?
      

  8.   

    大多数传输标准都是XML的,web.config 均是,你自己写个XML算了,再写个CLass来读取
      

  9.   

    web.config也是xml文件
    自己写一个xml文件,改后缀名为config
    在你自己写的xml文件怎么配置都行的
      

  10.   

    UPDATE:You can also download the demo project from CodeProjecthttp://www.codeproject.com/KB/aspnet/CustomXmlConfigForAspNet/Huobaz.XiaHouWeni.CSDN.TestWebapplication.zip
    http://www.codeproject.com/KB/aspnet/CustomXmlConfigForAspNet.aspx