Write an INI file in C#

Add a using statement

using System.Runtime.InteropServices;

Example Code

        public class IniWriter
        {
            #region | Public Properites 
            /// <summary>
            /// Gets or Sets the File Path to the settings file.
            /// </summary>
            public string FilePath { get; set; }
            #endregion
 
            public const int DEFAULT_SIZE = 255;
 
            #region |DLL IMPORTS |
            [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);
	        #endregion
 
            #region | Constructors |
            /// <summary>
            /// Intialize a new instance of <see cref="IniWriter"/>
            /// </summary>
            /// <param name="filepath"></param>
            public IniWriter(string filepath)
            {
                this.FilePath = filepath;
            }
 
            #endregion
 
            #region | Public Methods |
            public void WriteValue(string Section, string Key, string Value)
            {
                WritePrivateProfileString(Section, Key, Value, this.FilePath);
            }
 
            public string ReadValue(string Section, string Key)
            {
                StringBuilder temp = new StringBuilder(IniWriter.DEFAULT_SIZE);
                int i = GetPrivateProfileString(Section, Key, "", temp , 255, this.FilePath);
                return temp.ToString();
            }
            #endregion
 
        }

Your rating: None Average: 3.5 (2 votes)