实例介绍
【实例截图】
【核心代码】
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; using IPMessagerNet._Embed; namespace IPMessagerNet.Core { static class ProfileManager { //配置文件存储路径 static string ProfilePath; static string BinPath; static ProfileManager() { ProfilePath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "飞鸽传书.Net"); BinPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); } #region 存取配置文件 /// <summary> /// 加载配置 /// </summary> /// <typeparam name="T">配置类型</typeparam> /// <returns></returns> public static T LoadConfig<T>() where T : class, new() { Type t = typeof(T); string path = System.IO.Path.Combine(ProfilePath, t.Name ".xml"); if (!System.IO.File.Exists(path)) return null; else { T obj = null; using (System.IO.StreamReader sr = new System.IO.StreamReader(path, System.Text.Encoding.Unicode)) { System.Xml.Serialization.XmlSerializer xml = new System.Xml.Serialization.XmlSerializer(t); obj = xml.Deserialize(sr) as T; sr.Close(); } return obj; } } /// <summary> /// 加载配置 /// </summary> /// <typeparam name="T">配置类型</typeparam> /// <returns></returns> public static object LoadConfig(Type t, string configName) { string path = System.IO.Path.Combine(ProfilePath, configName ".xml"); if (!System.IO.File.Exists(path)) return null; else { object obj = null; using (System.IO.StreamReader sr = new System.IO.StreamReader(path, System.Text.Encoding.Unicode)) { System.Xml.Serialization.XmlSerializer xml = new System.Xml.Serialization.XmlSerializer(t); obj = xml.Deserialize(sr); sr.Close(); } return obj; } } /// <summary> /// 保存配置 /// </summary> /// <param name="cfg">要保存的配置对象</param> public static void SaveConfig(object cfg) { if (cfg == null) throw new ArgumentNullException(); Type t = cfg.GetType(); string path = System.IO.Path.Combine(ProfilePath, t.Name ".xml"); System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(path)); using (System.IO.StreamWriter sr = new System.IO.StreamWriter(path, false, System.Text.Encoding.Unicode)) { System.Xml.Serialization.XmlSerializer xml = new System.Xml.Serialization.XmlSerializer(t); xml.Serialize(sr, cfg); sr.Close(); } } /// <summary> /// 保存配置 /// </summary> /// <param name="cfg">要保存的配置对象</param> public static void SaveConfig(string configName, object cfg) { if (cfg == null) throw new ArgumentNullException(); Type t = cfg.GetType(); string path = System.IO.Path.Combine(ProfilePath, configName ".xml"); System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(path)); using (System.IO.StreamWriter sr = new System.IO.StreamWriter(path, false, System.Text.Encoding.Unicode)) { System.Xml.Serialization.XmlSerializer xml = new System.Xml.Serialization.XmlSerializer(t); xml.Serialize(sr, cfg); sr.Close(); } } #endregion #region 存取资源文件 /// <summary> /// 获得主题所在文件夹位置 /// </summary> /// <returns></returns> public static string GetThemeFolderRoot() { return string.Format("{0}{1}Themes{1}", BinPath, System.IO.Path.DirectorySeparatorChar); } /// <summary> /// 获得主题资源文件夹位置 /// </summary> /// <returns></returns> public static string GetThemeFolder() { return string.Format("{0}{2}Themes{2}{1}{2}", BinPath, Env.ClientConfig.Themes, System.IO.Path.DirectorySeparatorChar); } /// <summary> /// 获得资源图片位置 /// </summary> /// <typeparam name="T">资源类型</typeparam> /// <returns></returns> public static string GetThemePicturePath<T>() { return string.Format("{0}{2}Themes{2}{1}{2}Toolbar{2}{3}.png", BinPath, Env.ClientConfig.Themes, System.IO.Path.DirectorySeparatorChar, typeof(T).Name); } /// <summary> /// 获得资源图片位置 /// </summary> /// <returns></returns> public static string GetThemeFilePath(string catName, string fileName) { return string.Format("{0}{2}Themes{2}{1}{2}{3}{2}{4}{5}", BinPath, Env.ClientConfig.Themes, System.IO.Path.DirectorySeparatorChar, catName, fileName, fileName.IndexOf(".") > 0 ? "" : ".png"); } /// <summary> /// 获得资源图标位置 /// </summary> /// <typeparam name="T">资源类型</typeparam> /// <returns></returns> public static string GetThemeIconPath<T>() { return string.Format("{0}{2}Themes{2}{1}{2}FormIcon{2}{3}.ico", BinPath, Env.ClientConfig.Themes, System.IO.Path.DirectorySeparatorChar, typeof(T).Name); } /// <summary> /// 获得资源图标位置 /// </summary> /// <returns></returns> public static string GetThemeIconPath(string catName, string fileName) { return string.Format("{0}{2}Themes{2}{1}{2}{3}{2}{4}.ico", BinPath, Env.ClientConfig.Themes, System.IO.Path.DirectorySeparatorChar, catName, fileName); } /// <summary> /// 获得资源图片(Alert图标) /// </summary> /// <returns></returns> public static System.Drawing.Image GetThemePicture_Alert() { return GetThemePicture("Signal", "Alert"); } /// <summary> /// 获得资源图片 /// </summary> /// <returns></returns> public static System.Drawing.Image GetThemePicture(string catName, string fileName) { string path = GetThemeFilePath(catName, fileName); if (System.IO.File.Exists(path)) return ImageHelper.LoadFromFile(path); else return null; } /// <summary> /// 获得资源图片 /// </summary> /// <returns></returns> public static System.Drawing.Image GetThemePicture(this object obj, string cata) { string path = GetThemeFilePath(cata, obj.GetType().Name); if (System.IO.File.Exists(path)) return ImageHelper.LoadFromFile(path); else return null; } /// <summary> /// 获得资源图标 /// </summary> /// <returns></returns> public static System.Drawing.Icon GetThemeIcon(string catName, string fileName) { string path = GetThemeIconPath(catName, fileName); if (System.IO.File.Exists(path)) return IconHelper.LoadIcon(path); else return null; } /// <summary> /// 获得资源图标 /// </summary> /// <returns></returns> public static System.Drawing.Icon GetThemeIcon<T>() { string path = GetThemeIconPath<T>(); if (System.IO.File.Exists(path)) return IconHelper.LoadIcon(path); else return null; } /// <summary> /// 获得资源图标 /// </summary> /// <returns></returns> public static System.Drawing.Icon GetThemeIcon(this object obj) { return GetThemeIcon("FormIcon", obj.GetType().Name); } #endregion } }
标签: 通讯
相关软件
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论