www.pudn.com > WebPager.rar > ResManager.cs, change:2005-01-24,size:2731b


using System; 
using System.IO; 
using System.Drawing; 
using System.Resources; 
 
namespace Hooyee.Utils { 
	#region Class ResManager 
	/// <summary> 
	/// 资源管理器。 
	/// </summary> 
	public sealed class ResManager { 
		private ResManager() { 
		} 
 
		#region GetResource 
		/// <summary> 
		/// 用来存储资源文件中的文件内容 
		/// </summary> 
		private static NameObjectCollection _resources; 
 
		/// <summary> 
		/// 载入资源文件中的所有内容 
		/// </summary> 
		/// <param name="filename">资源文件名称</param> 
		/// <returns>返回 filename 文件中的内容</returns> 
		/// <remarks> 
		/// 此文件为 Resx 内部使用,仅供 LoadFile 文件调用 
		/// </remarks> 
		public static byte[] GetResource(string filename) { 
			return GetResource( typeof(ResManager), filename ); 
		} 
 
		/// <summary> 
		/// 载入资源文件中的所有内容 
		/// </summary> 
		/// <param name="assemblyType">资源所在程序集的某一类型</param> 
		/// <param name="filename">资源文件名称</param> 
		/// <returns>返回 filename 文件中的内容</returns> 
		/// <remarks> 
		/// 此文件为 Resx 内部使用,仅供 LoadFile 文件调用 
		/// </remarks> 
		public static byte[] GetResource(Type assemblyType, string filename) { 
			if(assemblyType == null) 
				throw(new ArgumentNullException("assemblyType")); 
			if(filename == null) 
				throw(new ArgumentNullException("filename")); 
 
			if ( _resources == null ) 
				_resources = new NameObjectCollection(); 
 
			if ( _resources.Contains(filename) == false ){ 
				byte[] buffer; 
				using (Stream strm = assemblyType.Assembly 
						   .GetManifestResourceStream(filename)) { 
					if ( null == strm ) 
						return null; 
 
					buffer = new byte[strm.Length]; 
					strm.Read(buffer, 0, buffer.Length); 
				} 
				_resources.Add(filename, buffer); 
			} 
 
			return (byte[]) _resources[filename];			 
		} 
		#endregion 
 
		#region GetImage 
		/// <summary> 
		/// 得到Image文件内容 
		/// </summary> 
		/// <param name="filename">文件名</param> 
		/// <returns></returns> 
		public static Image GetImage(string filename) { 
			byte[] res = GetResource(filename); 
			if(res == null || res.Length.Equals(0)) 
				throw new ArgumentNullException("filename","isn`t find "+filename); 
			Image img  = Image.FromStream(new MemoryStream(res)); 
			return img; 
		} 
		#endregion 
 
		#region GetIcon 
		/// <summary> 
		/// 得到Icon文件内容 
		/// </summary> 
		/// <param name="filename">文件名</param> 
		/// <returns></returns> 
		public static Icon GetIcon(string filename){ 
			byte[] res = GetResource(filename); 
			if(res == null || res.Length.Equals(0)) 
				throw new ArgumentNullException("filename","isn`t find "+filename); 
			return new Icon( new MemoryStream(res) ); 
		} 
		#endregion 
	} 
	#endregion 
}