1. 程式人生 > >C# read write ini file

C# read write ini file

  [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);
        /// <summary> 
        /// 寫入INI檔案 
        /// </summary> 
        /// <param name="Section">專案名稱(如 [TypeName] )</param> 
        /// <param name="Key">鍵</param> 
        /// <param name="Value">值</param> 
  
        public void IniWriteValue(string Section, string Key, string Value)
        {
            WritePrivateProfileString(Section, Key, Value, this.inipath);
        }
        ///  /// <summary> 
        /// 讀出INI檔案 
        /// </summary> 
        /// <param name="Section">專案名稱(如 [TypeName] )</param> 
        /// <param name="Key">鍵</param> 
        public string IniReadValue(string Section, string Key)
        {
            StringBuilder temp = new StringBuilder(500);
            int i = GetPrivateProfileString(Section, Key, "", temp, 500, this.inipath);
            return temp.ToString();
        }

        public MainWindow()
        {
            InitializeComponent();

            MessageBox.Show(IniReadValue("App","CanMultiRun"));
           // IniWriteValue ("App", "CanMultiRun","1");
            MessageBox.Show(IniReadValue("App", "CanMultiRun2"));


        }
        string inipath = System.IO.Path.Combine ( AppDomain.CurrentDomain.BaseDirectory, "AppConfig");