h1.post-title { color:orange; font-family:verdana,Arial; font-weight:bold; padding-bottom:5px; text-shadow:#64665b 0px 1px 1px; font-size:32px; } -->

Pages

Create INI File in C#.Net

Today , we will discuss about Create,Read and Write INI File in C#.Net


Create a Class file for INI File Operation

Class file name : INIOperation.cs
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Security.AccessControl;
using System.Text;
 #region Declarations
        private string path = string.Empty;
        [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);
        int count = 0;
        private static string _SDevice = string.Empty;
string GetInputPath  =string.Empty;
string GetOutputPath =string.Empty;
        #endregion


     #region Constructor
        /// <summary>
        /// INI Operation
        /// </summary>
        /// <param name="INIPath">Pass INI Path</param>
        public INIOperation(string INIPath)
        {
            if (!File.Exists(INIPath))
            {
                path = INIPath;
            }
            else
            {
                File.SetAttributes(INIPath, FileAttributes.Normal);
                File.Delete(INIPath);
                path = INIPath;
            }
        }
        /// <summary>
        /// INI OPperation
        /// </summary>
        /// <param name="INIPath">Pass INI Path</param>
        /// <param name="mode">Pass Mode [Read]</param>
        public INIOperation(string INIPath, string mode)
        {
            if (mode == "Read")
            {
                path = INIPath;
            }
        }
        #endregion Constructor

        #region Functions/Methods    
        /// <summary>
        /// Write Data to the INI File
        /// </summary>
        /// <PARAM name="Section"></PARAM>
        /// Section name
        /// <PARAM name="Key"></PARAM>
        /// Key Name
        /// <PARAM name="Value"></PARAM>
        /// Value Name
        public void IniWriteValue(string Section, string Key, string Value)
        {
            WritePrivateProfileString(Section, Key, Value, this.path);
        }
        /// <summary>
        /// Read Data Value From the Ini File
        /// </summary>
        /// <PARAM name="Section"></PARAM>
        /// <PARAM name="Key"></PARAM>
        /// <PARAM name="Path"></PARAM>
        /// <returns></returns>
        public string IniReadValue(string Section, string Key)
        {
            StringBuilder temp = new StringBuilder(255);
            int i = GetPrivateProfileString(Section, Key, "", temp, 255, this.path);
            return temp.ToString();
        }



   /// <summary>
        /// Default INI File
        /// </summary>
        public void DefaultINI()
        {
            IniWriteValue("InputPath", "Input", Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + @"\EasyconWS\PCL");
            IniWriteValue("OutputPath", "Output", Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + @"\EasyconWS\Data");
       
        }
        /// <summary>
        /// Read Ini File
        /// </summary>
        public void ReadINI()
        {
            GetInputPath = IniReadValue("InputPath", "Input");
            GetOutputPath = IniReadValue("OutputPath", "Output");        
        }