四、读取配置
也是最关系的,配置信息怎么读出来。在上面类写好后读取非常容易了,只需用WebConfigurationManager.OpenWebConfiguration("~"). GetSection()获取配置节的实例,局可以使用属性值了
var _uploadConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~").GetSection("UploadConfig") as Ninesky.Models.Config.UploadConfig; //文件最大限制 int _maxSize = _uploadConfig.MaxSize; //文件路径 string _fileParth = _uploadConfig.Path ; string _dirName; //允许上传的类型 Hashtable extTable = new Hashtable(); extTable.Add("image", _uploadConfig.ImageExt); extTable.Add("flash", _uploadConfig.FileExt); extTable.Add("media", _uploadConfig.MediaExt); extTable.Add("file", _uploadConfig.FileExt);
五、写入配置
跟读取类似,把属性值设置后save()一下,内容就会保存到Config\Upload.config中。
代码如下:
var _uploadConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~").GetSection("UploadConfig") as Ninesky.Models.Config.UploadConfig; //文件最大限制 int _maxSize = _uploadConfig.MaxSize; _uploadConfig.FileExt = "doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2"; _uploadConfig.FlashExt = "swf,flv"; _uploadConfig.ImageExt = "gif,jpg,jpeg,png,bmp"; _uploadConfig.MediaExt = "swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb"; _uploadConfig.Path = "Upload"; _uploadConfig.CurrentConfiguration.Save();