//保存窗口字体到ini文件 public void saveFont(Form wnd, String fontName, int fontSize) { if (null == wnd) { return; } fontName = CSTR.trim(fontName); if (fontName.Length <= 0) { return; } if (fontSize <= 3 || fontSize > 100) { return; } //最大化时不保存大小和位置,而是保留上一次非最大化时的值 try { //wnd.Font = new System.Drawing.Font(fontName, fontSize); INI_FILE.INIWriteValue(ini_file, "params", "font_family", fontName); INI_FILE.INIWriteValue(ini_file, "params", "font_size", fontSize.ToString()); } catch (Exception exp) { Console.WriteLine(exp.Message); } }
private String ini_file_getParamValue(String strName) { strName = CSTR.trim(strName); if (strName.Length <= 0) { return(""); } String strRet = ""; try { //确保ini文件存在,没有则创建 ini_file_exist_suarance(); //读取ini文件,并更新g_version strRet = CSTR.trim(INI_FILE.INIGetStringValue(ini_file, "params", strName, null)); } catch (Exception exp) { Console.WriteLine(exp.Message); strRet = ""; } return(strRet); }
/// <summary> /// 读取INI文件中指定KEY的字符串型值 /// </summary> /// <param name="iniFile">Ini文件</param> /// <param name="section">节点名称</param> /// <param name="key">键名称</param> /// <param name="defaultValue">如果没此KEY所使用的默认值</param> /// <returns>读取到的值</returns> public static string INIGetStringValue(string iniFile, string section, string key, string defaultValue) { string value = defaultValue; const int SIZE = 1024 * 10; if (string.IsNullOrEmpty(section)) { throw new ArgumentException("必须指定节点名称", "section"); } if (string.IsNullOrEmpty(key)) { throw new ArgumentException("必须指定键名称(key)", "key"); } StringBuilder sb = new StringBuilder(SIZE); uint bytesReturned = INI_FILE.GetPrivateProfileString(section, key, defaultValue, sb, SIZE, iniFile); if (bytesReturned != 0) { value = sb.ToString(); } sb = null; return value; }
//保存窗口状态值到ini文件 public void save(Form wnd) { if (null == wnd) { return; } bool isMaximized = false; if (wnd.WindowState == FormWindowState.Maximized) { isMaximized = true; } //最大化时不保存大小和位置,而是保留上一次非最大化时的值 try { if (isMaximized) { INI_FILE.INIWriteValue(ini_file, "params", "is_maximized", isMaximized.ToString()); } else { //当前屏幕分辨率 int screenWidth = Screen.PrimaryScreen.Bounds.Width; int screenHeight = Screen.PrimaryScreen.Bounds.Height; //如果窗口位置超出屏幕的有效范围,则不予保存 if (wnd.Left < 0 || wnd.Left > screenWidth) { return; } if (wnd.Top < 0 || wnd.Top > screenHeight) { return; } if (wnd.Width <= 0 || wnd.Width > screenWidth) { return; } if (wnd.Height <= 0 || wnd.Height > screenHeight) { return; } INI_FILE.INIWriteValue(ini_file, "params", "top", wnd.Top.ToString()); INI_FILE.INIWriteValue(ini_file, "params", "left", wnd.Left.ToString()); INI_FILE.INIWriteValue(ini_file, "params", "width", wnd.Width.ToString()); INI_FILE.INIWriteValue(ini_file, "params", "height", wnd.Height.ToString()); INI_FILE.INIWriteValue(ini_file, "params", "is_maximized", isMaximized.ToString()); } } catch (Exception exp) { Console.WriteLine(exp.Message); } }
/// <summary> /// 在INI文件中,删除指定节点中的所有内容。 /// </summary> /// <param name="iniFile">INI文件</param> /// <param name="section">节点</param> /// <returns>操作是否成功</returns> public static bool INIEmptySection(string iniFile, string section) { if (string.IsNullOrEmpty(section)) { throw new ArgumentException("必须指定节点名称", "section"); } return INI_FILE.WritePrivateProfileSection(section, string.Empty, iniFile); }
/// <summary> /// 在INI文件中,删除指定节点中的指定的键。 /// </summary> /// <param name="iniFile">INI文件</param> /// <param name="section">节点</param> /// <param name="key">键</param> /// <returns>操作是否成功</returns> public static bool INIDeleteKey(string iniFile, string section, string key) { if (string.IsNullOrEmpty(section)) { throw new ArgumentException("必须指定节点名称", "section"); } if (string.IsNullOrEmpty(key)) { throw new ArgumentException("必须指定键名称", "key"); } return INI_FILE.WritePrivateProfileString(section, key, null, iniFile); }
/// <summary> /// 在INI文件中,将指定的键值对写到指定的节点,如果已经存在则替换 /// </summary> /// <param name="iniFile">INI文件</param> /// <param name="section">节点,如果不存在此节点,则创建此节点</param> /// <param name="items">键值对,多个用\0分隔,形如key1=value1\0key2=value2</param> /// <returns></returns> public static bool INIWriteItems(string iniFile, string section, string items) { if (string.IsNullOrEmpty(section)) { throw new ArgumentException("必须指定节点名称", "section"); } if (string.IsNullOrEmpty(items)) { throw new ArgumentException("必须指定键值对", "items"); } return INI_FILE.WritePrivateProfileSection(section, items, iniFile); }
//用于QueryBench的附加配置,控制是否需要需要手环嘀卡实现放射科自动登记到检 public int QueryBench_is_need_ris_auto_register() { String item_name = "need_ris_auto_register"; String str = ini_file_getParamValue(item_name); if (str.Length <= 0) { //如果原来的ini文件无此Item,则增加一个带默认值的选项 INI_FILE.INIWriteValue(ini_file, "params", item_name, "0"); return(0); } return(strToInt(str)); }
//用于DocBench和TijianRing的附加配置,控制是否需要本地语音 public int workstation_is_need_local_voice() { String item_name = "need_local_voice"; String str = ini_file_getParamValue(item_name); if (str.Length <= 0) { //如果原来的ini文件无此Item,则增加一个带默认值的选项 INI_FILE.INIWriteValue(ini_file, "params", item_name, "0"); return(0); } return(strToInt(str)); }
private void ini_file_setVersion(String strVersion) { strVersion = CSTR.trim(strVersion); if (strVersion.Length <= 0) { strVersion = "0"; } try { INI_FILE.INIWriteValue(ini_file, "params", "version", strVersion); INI_FILE.INIWriteValue(ini_file, "params", "update_date", DateTime.Now.ToString()); } catch (Exception exp) { Console.WriteLine(exp.Message); } }
/// <summary> /// 在INI文件中,指定节点写入指定的键及值。如果已经存在,则替换。如果没有则创建。 /// </summary> /// <param name="iniFile">INI文件</param> /// <param name="section">节点</param> /// <param name="key">键</param> /// <param name="value">值</param> /// <returns>操作是否成功</returns> public static bool INIWriteValue(string iniFile, string section, string key, string value) { if (string.IsNullOrEmpty(section)) { throw new ArgumentException("必须指定节点名称", "section"); } if (string.IsNullOrEmpty(key)) { throw new ArgumentException("必须指定键名称", "key"); } if (value == null) { throw new ArgumentException("值不能为null", "value"); } return INI_FILE.WritePrivateProfileString(section, key, value, iniFile); }
/// <summary> /// 获取INI文件中指定节点(Section)中的所有条目的Key列表 /// </summary> /// <param name="iniFile">Ini文件</param> /// <param name="section">节点名称</param> /// <returns>如果没有内容,反回string[0]</returns> public static string[] INIGetAllItemKeys(string iniFile, string section) { string[] value = new string[0]; const int SIZE = 1024 * 10; if (string.IsNullOrEmpty(section)) { throw new ArgumentException("必须指定节点名称", "section"); } char[] chars = new char[SIZE]; uint bytesReturned = INI_FILE.GetPrivateProfileString(section, null, null, chars, SIZE, iniFile); if (bytesReturned != 0) { value = new string(chars).Split(new char[] { '\0' }, StringSplitOptions.RemoveEmptyEntries); } chars = null; return value; }
//如果没有ini文件则重新生成一个 private void ini_file_exist_suarance() { try { //判断ini文件是否存在 if (!System.IO.File.Exists(ini_file)) { //生成并写入默认值到ini文件 //写入默认值得 INI_FILE.INIWriteValue(ini_file, "params", "top", "0"); INI_FILE.INIWriteValue(ini_file, "params", "left", "0"); INI_FILE.INIWriteValue(ini_file, "params", "width", "0"); INI_FILE.INIWriteValue(ini_file, "params", "height", "0"); INI_FILE.INIWriteValue(ini_file, "params", "is_maximized", "false"); INI_FILE.INIWriteValue(ini_file, "params", "font_family", "宋体"); INI_FILE.INIWriteValue(ini_file, "params", "font_size", "18"); } } catch (Exception exp) { Console.WriteLine(exp.Message); } }
/// <summary> /// 获取INI文件中指定节点(Section)中的所有条目(key=value形式) /// </summary> /// <param name="iniFile">Ini文件</param> /// <param name="section">节点名称</param> /// <returns>指定节点中的所有项目,没有内容返回string[0]</returns> public static string[] INIGetAllItems(string iniFile, string section) { //返回值形式为 key=value,例如 Color=Red uint MAX_BUFFER = 32767; //默认为32767 string[] items = new string[0]; //返回值 //分配内存 IntPtr pReturnedString = Marshal.AllocCoTaskMem((int)MAX_BUFFER * sizeof(char)); uint bytesReturned = INI_FILE.GetPrivateProfileSection(section, pReturnedString, MAX_BUFFER, iniFile); if (!(bytesReturned == MAX_BUFFER - 2) || (bytesReturned == 0)) { string returnedString = Marshal.PtrToStringAuto(pReturnedString, (int)bytesReturned); items = returnedString.Split(new char[] { '\0' }, StringSplitOptions.RemoveEmptyEntries); } Marshal.FreeCoTaskMem(pReturnedString); //释放内存 return items; }
/// <summary> /// 读取INI文件中指定INI文件中的所有节点名称(Section) /// </summary> /// <param name="iniFile">Ini文件</param> /// <returns>所有节点,没有内容返回string[0]</returns> public static string[] INIGetAllSectionNames(string iniFile) { uint MAX_BUFFER = 32767; //默认为32767 string[] sections = new string[0]; //返回值 //申请内存 IntPtr pReturnedString = Marshal.AllocCoTaskMem((int)MAX_BUFFER * sizeof(char)); uint bytesReturned = INI_FILE.GetPrivateProfileSectionNames(pReturnedString, MAX_BUFFER, iniFile); if (bytesReturned != 0) { //读取指定内存的内容 string local = Marshal.PtrToStringAuto(pReturnedString, (int)bytesReturned).ToString(); //每个节点之间用\0分隔,末尾有一个\0 sections = local.Split(new char[] { '\0' }, StringSplitOptions.RemoveEmptyEntries); } //释放内存 Marshal.FreeCoTaskMem(pReturnedString); return sections; }