Chinaunix首页 | 论坛 | 博客
  • 博客访问: 482286
  • 博文数量: 35
  • 博客积分: 4010
  • 博客等级: 上校
  • 技术积分: 1234
  • 用 户 组: 普通用户
  • 注册时间: 2005-10-06 22:48
文章分类

全部博文(35)

文章存档

2008年(35)

我的朋友

分类: C/C++

2008-11-18 10:08:01

  最近做的东西需要在DotNet下读取ini文件,C的可以直接调用WritePrivateProfileString,GetPrivateProfileSection等win32Api,在DotNet没找到好用的:<,本来打算写一个类专门是DotNet下操作ini文件的,结果网上搜资料的时候,搜到一个类,挺好用~呵:>!!把资料在Blog上记录一下。

public class IniFile
    {
        private const uint StringBufferSize = 32768;

        public IniFile(string filePath)
        {
            FilePath = filePath;
            Enc = Encoding.Default;
        }

        public IniFile(string filePath, Encoding enc)
        {
            FilePath = filePath;
            Enc = enc;
        }

        [DllImport("kernel32.dll",
                EntryPoint = "GetPrivateProfileSectionNames")]
        private static extern uint GetPrivateProfileSectionNames(
                sbyte[] lpszReturnBuffer, uint nSize, string lpFileName);

        public List<string> GetSectionNames()
        {
            sbyte[] lpszReturnBuffer = new sbyte[StringBufferSize];
            uint len = GetPrivateProfileSectionNames(lpszReturnBuffer,
                    (uint)lpszReturnBuffer.Length, FilePath);

            List<string> sections = new List<string>();
            unsafe
            {
                fixed (sbyte* pBuf = lpszReturnBuffer)
                {
                    uint i = 0;
                    while (i < len)
                    {
                        uint start = i;
                        uint length = 0;

                        while (i < len && lpszReturnBuffer[i] != 0)
                        {
                            ++i;
                        }

                        length = i - start;
                        sections.Add(new string(pBuf,
                                (int)start, (int)length, Enc));

                        ++i;
                        if (i < len && lpszReturnBuffer[i] == 0)
                        {
                            break;
                        }
                    }
                }
            }

            return sections;
        }

        [DllImport("kernel32.dll", EntryPoint = "GetPrivateProfileSection")]
        private static extern uint GetPrivateProfileSection(string lpAppName,
                sbyte[] lpReturnedString, uint nSize, string lpFileName);

        public List<string> GetSection(string section)
        {
            sbyte[] lpReturnedString = new sbyte[StringBufferSize];
            uint len = GetPrivateProfileSection(section,
                  lpReturnedString, (uint)lpReturnedString.Length, FilePath);

            List<string> items = new List<string>();
            unsafe
            {
                fixed (sbyte* pBuf = lpReturnedString)
                {
                    uint i = 0;
                    while (i < len)
                    {
                        uint start = i;
                        uint length = 0;

                        while (i < len && lpReturnedString[i] != 0)
                        {
                            ++i;
                        }

                        length = i - start;
                        items.Add(new string(pBuf,
                                (int)start, (int)length, Enc));

                        ++i;
                        if (i < len && lpReturnedString[i] == 0)
                        {
                            break;
                        }
                    }
                }
            }

            return items;
        }

        public Dictionary<string, string> GetSetctionByPair(string section)
        {
            Dictionary<string, string> sectionDictionary = new Dictionary<string, string>();

            List<string> sectionList = GetSection(section);

            foreach (string value in sectionList)
            {
                string mark = "=";
                char[] splitMark = mark.ToCharArray();
                sectionDictionary.Add(value.Split(splitMark, 2)[0], value.Split(splitMark, 2)[1]);
            }

            return sectionDictionary;
        }

        [DllImport("kernel32", EntryPoint = "GetPrivateProfileString")]
        private static extern uint GetPrivateProfileString(
                string lpAppName, string lpKeyName, string lpDefault,
                StringBuilder lpReturnedString, uint nSize, string lpFileName);

        public string GetString(string section, string key, string def)
        {

            StringBuilder value = new StringBuilder((int)StringBufferSize);
            GetPrivateProfileString(section, key,
                    def, value, StringBufferSize, FilePath);
            return value.ToString();
        }

        [DllImport("kernel32")]
        private static extern bool WritePrivateProfileSection(
                    string lpAppName,
                    string lpString,
                    string lpFileName
                );

        public bool WriteSection(string section, List<string> lstKeyValue)
        {
            string lpString = " ";
            for (int i = 0; i < lstKeyValue.Count; ++i)
            {
                lpString += lstKeyValue[i];
                lpString += "\0 ";
            }

            lpString += "\0 ";

            return WritePrivateProfileSection(section, lpString, FilePath);
        }

        [DllImport("kernel32", EntryPoint = "WritePrivateProfileString")]
        private static extern bool WritePrivateProfileString(string lpAppName,
                string lpKeyName, string lpString, string lpFileName);

        public bool WriteString(string section, string key, string value)
        {
            return WritePrivateProfileString(section, key, value, FilePath);
        }

        private string FilePath;
        private Encoding Enc;
    }

阅读(925) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~