最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

c#實(shí)現(xiàn)ini文件讀寫類分享

 更新時(shí)間:2013年12月27日 16:07:17   作者:  
c#實(shí)現(xiàn)ini文件讀寫類分享,大家參考使用吧

復(fù)制代碼 代碼如下:

/// <summary>
    /// 讀寫INI文件的類。
    /// </summary>
    public class INIHelper
    {
        // 讀寫INI文件相關(guān)。
        [DllImport("kernel32.dll", EntryPoint = "WritePrivateProfileString", CharSet = CharSet.Ansi)]
        public static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
        [DllImport("kernel32.dll", EntryPoint = "GetPrivateProfileString", CharSet = CharSet.Ansi)]
        public static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

        [DllImport("kernel32.dll", EntryPoint = "GetPrivateProfileSectionNames", CharSet = CharSet.Ansi)]
        public static extern int GetPrivateProfileSectionNames(IntPtr lpszReturnBuffer, int nSize, string filePath);

        [DllImport("KERNEL32.DLL ", EntryPoint = "GetPrivateProfileSection", CharSet = CharSet.Ansi)]
        public static extern int GetPrivateProfileSection(string lpAppName, byte[] lpReturnedString, int nSize, string filePath);


        /// <summary>
        /// 向INI寫入數(shù)據(jù)。
        /// </summary>
        /// <PARAM name="Section">節(jié)點(diǎn)名。</PARAM>
        /// <PARAM name="Key">鍵名。</PARAM>
        /// <PARAM name="Value">值名。</PARAM>
        public static void Write(string Section, string Key, string Value, string path)
        {
            WritePrivateProfileString(Section, Key, Value, path);
        }


        /// <summary>
        /// 讀取INI數(shù)據(jù)。
        /// </summary>
        /// <PARAM name="Section">節(jié)點(diǎn)名。</PARAM>
        /// <PARAM name="Key">鍵名。</PARAM>
        /// <PARAM name="Path">值名。</PARAM>
        /// <returns>相應(yīng)的值。</returns>
        public static string Read(string Section, string Key, string path)
        {
            StringBuilder temp = new StringBuilder(255);
            int i = GetPrivateProfileString(Section, Key, "", temp, 255, path);
            return temp.ToString();
        }

        /// <summary>
        /// 讀取一個(gè)ini里面所有的節(jié)
        /// </summary>
        /// <param name="sections"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        public static int GetAllSectionNames(out string[] sections, string path)
        {
            int MAX_BUFFER = 32767;
            IntPtr pReturnedString = Marshal.AllocCoTaskMem(MAX_BUFFER);
            int bytesReturned = GetPrivateProfileSectionNames(pReturnedString, MAX_BUFFER, path);
            if (bytesReturned == 0)
            {
                sections = null;
                return -1;
            }
            string local = Marshal.PtrToStringAnsi(pReturnedString, (int)bytesReturned).ToString();
            Marshal.FreeCoTaskMem(pReturnedString);
            //use of Substring below removes terminating null for split
            sections = local.Substring(0, local.Length - 1).Split('\0');
            return 0;
        }

        /// <summary>
        /// 得到某個(gè)節(jié)點(diǎn)下面所有的key和value組合
        /// </summary>
        /// <param name="section"></param>
        /// <param name="keys"></param>
        /// <param name="values"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        public static int GetAllKeyValues(string section, out string[] keys, out string[] values, string path)
        {
            byte[] b = new byte[65535];

            GetPrivateProfileSection(section, b, b.Length, path);
            string s = System.Text.Encoding.Default.GetString(b);
            string[] tmp = s.Split((char)0);
            ArrayList result = new ArrayList();
            foreach (string r in tmp)
            {
                if (r != string.Empty)
                    result.Add(r);
            }
            keys = new string[result.Count];
            values = new string[result.Count];
            for (int i = 0; i < result.Count; i++)
            {
                string[] item = result[i].ToString().Split(new char[] { '=' });
                if (item.Length == 2)
                {
                    keys[i] = item[0].Trim();
                    values[i] = item[1].Trim();
                }
                else if (item.Length == 1)
                {
                    keys[i] = item[0].Trim();
                    values[i] = "";
                }
                else if (item.Length == 0)
                {
                    keys[i] = "";
                    values[i] = "";
                }
            }

            return 0;
        }

    }

相關(guān)文章

最新評論

剑川县| 沂水县| 河津市| 科技| 青川县| 香港| 安多县| 德令哈市| 泸溪县| 金溪县| 磴口县| 邮箱| 屯门区| 华坪县| 漯河市| 林甸县| 合水县| 河池市| 龙井市| 平湖市| 塔城市| 双峰县| 平遥县| 宝山区| 鹤岗市| 阿合奇县| 富源县| 池州市| 苏州市| 灌云县| 四会市| 沛县| 溧阳市| 江阴市| 阜南县| 措勤县| 高碑店市| 德格县| 黄大仙区| 库尔勒市| 瑞丽市|