1.首先.ini文件的編碼形式必須是Unicode,可通過文件另存為修改;
2.獲取配置文件路徑(注:我是放在.exe執行文件的根目錄下的)
CString sPath; CString StrName; StrName="dasdasda"; GetModuleFileName(NULL,sPath.GetBufferSetLength(MAX_PATH+1),MAX_PATH); sPath.ReleaseBuffer(); //到這一步獲取的是執行文件(.exe)的路徑 int nPos; nPos=sPath.ReverseFind('\\'); sPath=sPath.Left(nPos); CString lpszFile=sPath+"\\uilib.ini";//這里是配置文件路徑
3.寫入配置文件(若配置文件中已存在cityname,則會覆蓋它所對應的值)
WritePrivateProfileString(CString("AttenCitys"),CString("cityname"),StrName,lpszFile);
4.讀取配置文件
CString str; GetPrivateProfileString(CString("AttenCitys"),CString("cityname"),CString("NULL"),str.GetBuffer(),MAX_PATH,lpszFile); str.ReleaseBuffer();