100分悬赏:如何自定义使用config文件
像Discuz 里面大量使用config文件一样
给相关实例参考的 即送分 ; 不是要怎么操作xml的,这个我会,谢谢了
------解决方案--------------------
<?xml version="1.0" encoding= "utf-8" ?>
<configuration>
<appSettings>
<add key="DatabasePath" value="c:\\projects\data\spider.mdb" />
<add key="SupportEmail" value="webmaster-1@dotnetspider.com" />
</appSettings>
</configuration>
==========================================================================
.net 2.0 providing access method
string dbPath =
System.Configuration.ConfigurationSettings.AppSettings["DatabasePath"];
string email =
System.Configuration.ConfigurationSettings.AppSettings["SupportEmail"];
==========================================================================
------解决方案--------------------
把config做成实体
然后读取的时候序列化为实体,就可以了.
返回类型转为自己的实体
public static object Load(Type type, string filename)
{
FileStream fs = null;
try
{
// open the stream...
fs = new FileStream(GetMapPath(filename), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
XmlSerializer serializer = new XmlSerializer(type);
return serializer.Deserialize(fs);
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (fs != null)
fs.Close();
}
}
<SiteConfigInfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SiteName>网站名称</SiteName>
</SiteConfigInfo>
public class SiteConfigInfo
{
private string siteName;
public string SiteName
{
get { return siteName; }
set { siteName = value; }
}
}