Reading/writing an INI file

后端 未结 16 2378
南旧
南旧 2020-11-22 00:15

Is there any class in the .NET framework that can read/write standard .ini files:

[Section]
=
...

Delphi has th

16条回答
  •  灰色年华
    2020-11-22 00:46

    Here is my class, works like a charm :

    public static class IniFileManager
    {
    
    
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section,
            string key, string val, string filePath);
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section,
                 string key, string def, StringBuilder retVal,
            int size, string filePath);
        [DllImport("kernel32.dll")]
        private static extern int GetPrivateProfileSection(string lpAppName,
                 byte[] lpszReturnBuffer, int nSize, string lpFileName);
    
    
        /// 
        /// Write Data to the INI File
        /// 
        /// 
        /// Section name
        /// 
        /// Key Name
        /// 
        /// Value Name
        public static void IniWriteValue(string sPath,string Section, string Key, string Value)
        {
            WritePrivateProfileString(Section, Key, Value, sPath);
        }
    
        /// 
        /// Read Data Value From the Ini File
        /// 
        /// 
        /// 
        /// 
        /// 
        public static string IniReadValue(string sPath,string Section, string Key)
        {
            StringBuilder temp = new StringBuilder(255);
            int i = GetPrivateProfileString(Section, Key, "", temp,
                                            255, sPath);
            return temp.ToString();
    
        }
    

    }

    The use is obviouse since its a static class, just call IniFileManager.IniWriteValue for readsing a section or IniFileManager.IniReadValue for reading a section.

提交回复
热议问题