C#讀寫INI文件信息

using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;

public class IniFile
   {
        //文件INI名稱
        //public string Path;

        /**/////聲明讀寫INI文件的API函數
        [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);
        //類的構造函數,傳遞INI文件名
        //public IniFile(string inipath)
        //{
        //    //
        //    // TODO: Add constructor logic here
        //    //
        //    Path = inipath;
        //}

        //寫INI文件
        public void IniWriteValue(string Section, string Key, string Value,string Path)
        {
            WritePrivateProfileString(Section, Key, Value,Path);
        }

        /// <summary>
        /// 讀取INI文件指定的文件數據
        /// </summary>
        /// <param name="Section"></param>
        /// <param name="Key"></param>
        /// <param name="Path"></param>
        /// <returns></returns>
        public string IniReadValue(string Section, string Key,string Path)
       {
            StringBuilder temp = new StringBuilder(255);
            int i = GetPrivateProfileString(Section, Key, "", temp, 255, Path);
            return temp.ToString();
        }
        /**//// <summary>
        /// 驗證文件是否存在
        /// </summary>
        /// <returns>布爾值 </returns>
        //public bool ExistINIFile()
        //{
        //    return File.Exists(this.Path);
        //}

    }

//調用測試

向INI文件寫入數據

IniWriteValue("Login Information","Password ","73C18C59A39B3","F:/test.ini");

查看INI文件信息

[Login Information]
Password=73C18C59A39B3

//讀INI文件裏的Password值

String Password = IniFile.IniReadValue("User Information","Password","F:/test.ini");

 

http://hi.baidu.com/jonesvale/blog/item/8c374ea85bf727f41f17a22a.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章