c# ini文件操作


 

public class INIConfigHelper
    {
        public string Path;     //INI文件名
        [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文件的API函數     
        public INIConfigHelper(string iniPath)
        {
            Path = iniPath;
        }

        //類的構造函數,傳遞INI文件名
        public void IniWriteValue(string section, string key, string value)
        {
            WritePrivateProfileString(section, key, value, this.Path);
        }

        //讀INI文件         
        public string IniReadValue(string section, string key)
        {
            var temp = new StringBuilder(256);
            int i = GetPrivateProfileString(section, key, "", temp, 256, this.Path);
            return temp.ToString();
        }
    }
private INIConfigHelper configReader = new INIConfigHelper(string.Concat(AppDomain.CurrentDomain.BaseDirectory, "Config\\Counter.ini"));
讀取節點:
string section = "time";
                string startTimeStr = configReader.IniReadValue(section, "start");
更新節點:
configReader.IniWriteValue("time", "start", maxTime.ToString("yyyy-MM-dd HH:mm:ss"));

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM