zoukankan      html  css  js  c++  java
  • ini文件

    ini文件

    [section1]

    key1 = value1

    key2 = value2

    [section2]

    key1 = value1

    key2 = value2

        public class IniFileHelper
        {
                /*参数
            * 1,节:若传入null,第4个参数将会获得所有的section name
            * 2,key;若传入null,第4个参数将会获得指定的section下的所有的key name
            * 3,ini文件不存在指定key时,默认的返回的value,一般设置成空字符串
            * 4,value
            * 5,value的长度,应该设置成不小于ini文件中最长的value字符串长度
            * 6,要读取的ini文件的路径
            * 返回值
            * 读到的value的长度:key不存在或key对应的value为空,读到的为0
            */
            [DllImport("kernel32")]
            private static extern int GetPrivateProfileString(string sectionName, string key, string defaultValue, byte[] returnBuf, int size, string iniFilePath);
    
    
            /*参数
             * 1,节
             * 2,key,若传入null,则会移除指定的section
             * 3,value,若传入null,则会移除指定的key
             * 4,ini文件的路径
             */
            [DllImport("kernel32")]
            private static extern long WritePrivateProfileString(string section, string key, string value, string iniFilePath);
    
    
    
            /*功能
             * 返回ini文件指定section下的指定key对应的value
             * 参数
             * 1,节
             * 2,key
             * 3,ini文件路径
             * 4,待读取的ini文件采用的的编码类型
             * 返回值
             * value
             */
            public static string GetValue(string sectionName, string key, string iniFilePath, Encoding encoding)
            {
                byte[] Value = new byte[512];//ini文件内容相同,但是如果编码不同,则占用的byte数目不同
                int length = GetPrivateProfileString(sectionName, key, "", Value, 512, iniFilePath);
                if (length == 0)
                {
                    throw new Exception(string.Format("The value for section:{0} key:{1} doesn't exist in the inifile{2}", sectionName, key, iniFilePath));
                }
                string res = encoding.GetString(Value, 0, length);
                return res;
            }
    
            /*功能
             * 返回ini文件中所有的节
             * 参数
             * 1,ini文件路径
             * 2,ini文件的编码
             */
            public static List<string> GetSectionNames(string iniFilePath, Encoding encoding)
            {
                byte[] buffer = new byte[1024];
                int length = GetPrivateProfileString(null, null, "", buffer, 1024, iniFilePath);
                if (length == 0)
                {
                    throw new Exception(string.Format("No section doesn't exist in the inifile{0}", iniFilePath));
                }
                string[] rs = encoding.GetString(buffer, 0, length).Split(new string[] { "" }, StringSplitOptions.RemoveEmptyEntries);
                return rs.ToList();
            }
    
    
    
            /*功能
             * 返回指定节下的所有key的名称
             * 1,节
             * 2,ini文件的路径
             * 3,ini文件的编码
             */
            public static List<string> GetKeys(string sectionName, string iniFilePath, Encoding encoding)
            {
                byte[] buffer = new byte[2048];
                int length = GetPrivateProfileString(sectionName, null, "", buffer, 2048, iniFilePath);
                if (length == 0)
                {
                    throw new Exception(string.Format("No key doesn't exist for section:{0} in the inifile{1}", sectionName, iniFilePath));
                }
                string[] rs = encoding.GetString(buffer, 0, length).Split(new string[] { "" }, StringSplitOptions.RemoveEmptyEntries);
                return rs.ToList();
            }
    
    
    
            /*功能
             * 创捷key-value 或者 覆盖已存在的key的value
             */
            public static bool SetValue(string sectionName, string key, string value, string filePath)
            {
                int rs = (int)WritePrivateProfileString(sectionName, key, value, filePath);
                return rs > 0;
            }
            /*移除节
             */
            public static bool RemoveSection(string sectionName, string filePath)
            {
                int rs = (int)WritePrivateProfileString(sectionName, null, "", filePath);
                return rs > 0;
            }
            /*移除 key-value
             */
            public static bool Removekey(string sectionName, string key, string filePath)
            {
                int rs = (int)WritePrivateProfileString(sectionName, key, null, filePath);
                return rs > 0;
            }
        }
  • 相关阅读:
    UVa 128 Software CRC
    UVa 11258 String Partition(简单DP)
    POJ 3070 Fibonacci(矩阵乘法logN)
    UVa 10280 Old Wine Into New Bottles(剪枝+完全背包)
    图论笔记第四章 欧拉图与哈密尔顿图(beta.)考点
    图。。珍藏season
    图论及其应用哈密尔顿图(alpha)
    9.保健…todo
    android 移植笔记有感
    unp_exam_要点.doc
  • 原文地址:https://www.cnblogs.com/riversouth/p/10296664.html
Copyright © 2011-2022 走看看