configuration.cs

来自「wrox c#高级编程」· CS 代码 · 共 192 行

CS
192
字号
using System;
using System.IO;
using System.Web;
using System.Text;
// add a reference to System.Web assembly
using System.Web.Caching;
using System.Xml.Serialization;
using System.Xml;
using System.Configuration;

namespace Wrox.WebModules.MailingLists.Configuration
{
	public enum UnsubscrActionType
	{
		Remove = 0,
		SetInactive = 1
	}

	public class ModuleConfig
	{
		public static ModuleSettings GetSettings()
		{
			HttpContext context = HttpContext.Current;

			ModuleSettings data = (ModuleSettings)context.Cache["MailingLists_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("MailingLists_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 data)
		{
			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, data);
			fs.Close();
		}

		
		private static string GetSettingsFile()
		{
			HttpContext context = HttpContext.Current;
			// get the file path from the cache
			string filePath = (string)context.Cache["MailingLists_SettingsFile"];
			// if path is null, get it from web.config
			if (filePath == null)
			{
				// retrieve the value
				filePath=ConfigurationSettings.AppSettings["MailingLists_SettingsFile"];
				// save into the cache
				context.Cache["MailingLists_SettingsFile"] = filePath;
			}
			// return the path
			return filePath;
		}
	}
	
	
	public class ModuleSettings
	{		
		private string connectionString;
		private string subscribeUrl;
		private string senderName;
		private string senderEmail;
		private string newsSubject;
		private string signature;
		private string subscrSubject;
		private string subscrMessage;
		private string unsubscrSubject;
		private string unsubscrMessage;
		private bool sendSubscrEmail;
		private bool sendUnsubscrEmail;
		private UnsubscrActionType unsubscrAction;
		
		[XmlElement]
		public string ConnectionString
		{
			get { return connectionString; }
			set { connectionString = value; }
		}
		
		[XmlElement]
		public string SubscribeUrl
		{
			get { return subscribeUrl; }
			set { subscribeUrl = value; }
		}
		
		[XmlElement]
		public string SenderName
		{
			get { return senderName; }
			set { senderName = value; }
		}
		
		[XmlElement]
		public string SenderEmail
		{
			get { return senderEmail; }
			set { senderEmail = value; }
		}
		
		[XmlElement]
		public string NewsSubject
		{
			get { return newsSubject; }
			set { newsSubject = value; }
		}
		
		[XmlElement]
		public string Signature
		{
			get { return signature; }
			set { signature = value; }
		}
		
		[XmlElement]
		public string SubscrSubject
		{
			get { return subscrSubject; }
			set { subscrSubject = value; }
		}
		
		[XmlElement]
		public string SubscrMessage
		{
			get { return subscrMessage; }
			set { subscrMessage = value; }
		}
		
		[XmlElement]
		public string UnsubscrSubject
		{
			get { return unsubscrSubject; }
			set { unsubscrSubject = value; }
		}
		
		[XmlElement]
		public string UnsubscrMessage
		{
			get { return unsubscrMessage; }
			set { unsubscrMessage = value; }
		}
		
		[XmlElement]
		public bool SendSubscrEmail
		{
			get { return sendSubscrEmail; }
			set { sendSubscrEmail = value; }
		}
		
		[XmlElement]
		public bool SendUnsubscrEmail
		{
			get { return sendUnsubscrEmail; }
			set { sendUnsubscrEmail = value; }
		}

		[XmlElement]
		public UnsubscrActionType UnsubscrAction
		{
			get { return unsubscrAction; }
			set { unsubscrAction = value; }
		}
		
	}

}

⌨️ 快捷键说明

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