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.
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; } } }
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( ); } } }
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
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.
比如:config.cs
domain:abc.com
admin:zhnagsan
password:123456
.....
无非就是涉及对文件的操作而已
(使用保护文件安全性会高一些)
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; }
}
}
{
[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( );
}
}
}
{
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
自己写一个xml文件,改后缀名为config
在你自己写的xml文件怎么配置都行的
http://www.codeproject.com/KB/aspnet/CustomXmlConfigForAspNet.aspx