C# 讀取ini文件,讀不出來原因


先賦上相關讀取ini文件代碼

public class INIHelper
    {
        public string inipath;
        [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);

        public INIHelper(string INIPath)
        {
            inipath = INIPath;
        }

        public void set(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 ReadInivalue(string Section, string Key)
        {
            StringBuilder temp = new StringBuilder(500);
            int i = GetPrivateProfileString(Section, Key, "", temp, 500, this.inipath);
            return temp.ToString();
        }
        /// <summary> 
        /// 驗證文件是否存在 
        /// </summary> 
        /// <returns>布爾值</returns> 
        public bool ExistINIFile()
        {
            return File.Exists(inipath);
        }


        
    }

  調用以上方法的代碼

INIHelper iniHelper = new INIHelper(Application.StartupPath + "\\Level.ini");
private void ConfigureShowHighFrm_Load(object sender, EventArgs e)
        {
            if (iniHelper.ReadInivalue("config", "high") == "1")
                checkBox1.Checked = true;
            else
                checkBox1.Checked = false;

            if (iniHelper.ReadInivalue("config", "hit") == "1")
                checkBox2.Checked = true;
            else
                checkBox2.Checked = false;

            if (iniHelper.ReadInivalue("config", "low") == "1")
                checkBox3.Checked = true;
            else
                checkBox3.Checked = false;
        }

  然后再看ini文件截圖

不知道看到沒有這個圖片,這個就是ini文件第一行必須是空格,否則讀不出來哦!!!


免責聲明!

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



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