⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 moduleconfig.cs

📁 wrox c#高级编程
💻 CS
字号:
using System;
using System.IO;
using System.Text;
using System.Web;
using System.Web.Caching;
using System.Xml.Serialization;
using System.Xml;
using System.Configuration;

using Wrox.WebModules.AdsManager;
using Wrox.WebModules.AdsManager.Configuration;


namespace Wrox.WebModules.AdsManager.Configuration
{
	/// <summary>
	/// Summary description for ModuleConfig.
	/// </summary>
	public class ModuleConfig
	{
		public static ModuleSettings GetSettings()
		{
			HttpContext context = HttpContext.Current;

			ModuleSettings data = (ModuleSettings)context.Cache["AdsManager_Settings"];

			if (data == null)
			{
				XmlSerializer serializer = new XmlSerializer(typeof(ModuleSettings));
				try
				{
					string fileName = HttpContext.Current.Server.MapPath(GetSettingsFile());
					// create a filestream to read the XML document
					FileStream fs = new FileStream(fileName, FileMode.Open);	          
					// use the Deserialize method to retrieve the oject state
					data = (ModuleSettings)serializer.Deserialize(fs);
					fs.Close();
      				
					context.Cache.Insert("AdsManager_Settings", data, new CacheDependency(fileName));
				}
				catch (System.IO.FileNotFoundException)
				{
					// if the file is not found, return a new empty class
					data = new ModuleSettings();
				}
			}
			
			return data;
		}

		public static void SaveSettings(ModuleSettings settings)
		{
			string fileName = HttpContext.Current.Server.MapPath(GetSettingsFile());
			XmlSerializer serializer = new XmlSerializer (typeof(ModuleSettings));
        
			// serialize the object
			FileStream fs = new FileStream(fileName, FileMode.Create);
			serializer.Serialize(fs, settings);
			fs.Close();
		}

		private static string GetSettingsFile()
		{
			HttpContext context = HttpContext.Current;
			// get the file path from the cache
			string filePath = (string)context.Cache["AdsManager_SettingsFile"];
			// if path is null, get it from web.config
			if (filePath == null)
			{
				// retrieve the value
				filePath=ConfigurationSettings.AppSettings["AdsManager_SettingsFile"];
				// save into the cache
				context.Cache["AdsManager_SettingsFile"] = filePath;
			}
			// return the connection string
			return filePath;
		}		
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -