a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 編程 > C# > 正文

c#實現ini文件讀寫類分享

2020-01-24 02:57:41
字體:
來源:轉載
供稿:網友

復制代碼 代碼如下:

/// <summary>
    /// 讀寫INI文件的類。
    /// </summary>
    public class INIHelper
    {
        // 讀寫INI文件相關。
        [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寫入數據。
        /// </summary>
        /// <PARAM name="Section">節點名。</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數據。
        /// </summary>
        /// <PARAM name="Section">節點名。</PARAM>
        /// <PARAM name="Key">鍵名。</PARAM>
        /// <PARAM name="Path">值名。</PARAM>
        /// <returns>相應的值。</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>
        /// 讀取一個ini里面所有的節
        /// </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>
        /// 得到某個節點下面所有的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;
        }

    }

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 性色在线| 91欧美在线 | 国产一区二区三区免费观看 | 日本色网址| 日本在线观看 | 大陆毛片 | 男女靠逼的视频 | 密色视频 | 福利一区福利二区 | 久久精品视频免费看 | 成人国产在线观看 | 欧美黑人一区 | 五月婷婷丁香 | 二区视频 | 国产剧情一区二区三区 | 羞羞视频在线观看免费 | 97成人在线免费视频 | 99国产精品99久久久久久 | 91视频.www | 亚洲午夜精品一区二区三区他趣 | 丁香婷婷综合激情五月色 | 欧美不卡在线 | 久久免费看| 久久草视频 | 国产区第一页 | 日韩第1页 | 欧美寡妇偷汉性猛交 | 不卡日韩在线 | 国产一区二区自拍视频 | 欧美一区二区三区国产精品 | 中文字幕日韩欧美 | 国产精品美腿一区在线看 | 成人在线国产 | 亚洲黄色在线免费观看 | 欧美精品片| 色综合久久网 | 99re在线视频| 一区二区免费视频 | 羞羞视频在线播放 | 精品久久久久久久久久久 | 欧美中文一区 |