Exemple #1
0
        /// <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(INIOperationClass.WritePrivateProfileSection(section, string.Empty, iniFile));
        }
Exemple #2
0
 /// <summary>
 /// ini文件是以0,1作为checkBox状态,而类中是以Bool保存checkBox状态,此函数将bool类型转为0,1写入ini文件中
 /// </summary>
 /// <param name="b"></param>
 /// <param name="key"></param>
 public static void SetChkFile(string iniFile, string section, string key, bool b)
 {
     if (b == true)
     {
         Debug.WriteLine("SetChkFile:{0}", b);
         INIOperationClass.INIWriteValue(iniFile, section, key, "1");
     }
     else
     {
         Debug.WriteLine("SetChkFile:{0}", b);
         INIOperationClass.INIWriteValue(iniFile, section, key, "0");
     }
 }
Exemple #3
0
        /// <summary>
        /// 从ini文件中读取checkBox控件的初始化显示
        /// </summary>
        /// <param name="sender">哪个form上的CheckBox控件,通常为this</param>
        /// <param name="checkBoxName">checkBox的Name属性</param>
        /// <param name="iniFile">ini文件名</param>
        /// <param name="section">节点</param>
        /// <param name="key">读取指定的key</param>
        public static void CheckBoxLoad(object sender, string checkBoxName, string iniFile, string section, string key)
        {
            object o = sender.GetType().GetField(checkBoxName, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.IgnoreCase).GetValue(sender);

            if (INIOperationClass.INIGetStringValue(iniFile, section, key, "") == "0")
            {
                (o as CheckBox).Checked = false;
            }
            else if (INIOperationClass.INIGetStringValue(iniFile, section, key, "") == "1")
            {
                (o as CheckBox).Checked = true;
            }
        }
Exemple #4
0
        /// <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(INIOperationClass.WritePrivateProfileString(section, key, null, iniFile));
        }
Exemple #5
0
        /// <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(INIOperationClass.WritePrivateProfileSection(section, items, iniFile));
        }
Exemple #6
0
        /// <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(INIOperationClass.WritePrivateProfileString(section, key, value, iniFile));
        }
Exemple #7
0
        /// <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 = INIOperationClass.GetPrivateProfileString(section, null, null, chars, SIZE, iniFile);

            if (bytesReturned != 0)
            {
                value = new string(chars).Split(new char[] { '\0' }, StringSplitOptions.RemoveEmptyEntries);
            }
            chars = null;

            return(value);
        }
Exemple #8
0
        /// <summary>
        /// 从parameter.ini配置文件读取相关配置,对combBox控件进行初始化显示
        /// </summary>
        /// <param name="sender">对哪个form里的combBox进行初始化显示,通常为this</param>
        /// <param name="iniFile">ini文件</param>
        /// <param name="section">节点名字</param>
        /// <param name="key">传入"comset",表示comboBox初始化显示的key值</param>
        /// <param name="cmbName">comboBox Name属性</param>
        public static void CmbLoad(object sender, string iniFile, string section, string key, string cmbName)
        {
            string temp = INIOperationClass.INIGetStringValue(iniFile, section, "All", "1");

            Debug.WriteLine(temp);

            int comX_num = int.Parse(temp);

            object o = sender.GetType().GetField(cmbName, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.IgnoreCase).GetValue(sender);

            //Debug.WriteLine("comX_num={0}", comX_num);
            //不加这个if条件,它每打开一次该对话框就会往里面添加一次,因为他打开一次就会调用一次load函数
            if ((o as ComboBox).Items.Count != comX_num)
            {
                for (int i = 1; i <= comX_num; i++)
                {
                    (o as ComboBox).Items.Add(INIOperationClass.INIGetStringValue(iniFile, section, i.ToString(), "1"));
                }
            }
            (o as ComboBox).SelectedItem = INIOperationClass.INIGetStringValue(iniFile, section, key, "1");
        }
Exemple #9
0
        /// <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 = INIOperationClass.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);
        }
Exemple #10
0
        public static void TextBoxLoad(object sender, string TextBoxName, string iniFile, string section, string key)
        {
            object o = sender.GetType().GetField(TextBoxName, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.IgnoreCase).GetValue(sender);

            (o as TextBox).Text = INIOperationClass.INIGetStringValue(iniFile, section, key, "");
        }