1. 程式人生 > >VS2013中對配置檔案INI的操作

VS2013中對配置檔案INI的操作

        //類的標頭檔案中定義 CString m_strRadLedDan;變數

        GetModuleFileName(NULL, m_strRadLedDan.GetBuffer(MAX_PATH), MAX_PATH);

m_strRadLedDan.ReleaseBuffer();
m_strRadLedDan = m_strRadLedDan.Left(m_strRadLedDan.ReverseFind('\\') + 1);

m_strRadLedDan += "RadLed.ini";        //配置的INI檔名  在專案可執行檔案的目錄裡(Debug/Release資料夾裡)

        WritePrivateProfileString("AppName", “KeyName”,“KeyValue”, m_strRadLedDan);    //寫入檔案的組名,鍵名和鍵值,最後是寫入檔案的位置和檔名(“RadLed.ini”);寫入格式如下

//[AppName]

//KeyName = KeyValue

        CString str;

        GetPrivateProfileString(“AppName”, “KeyName”, NULL, str.GetBuffer(20), 20, m_strRadLedDan);//其中得到的s字串就是str,str 就是寫入的KeyValue,也可以是數值;

{//獲得已知組名下的內容(鍵名和鍵值)

static TCHAR szBuf[20000];
memset(szBuf, 0, 20000);
static TCHAR szWork[20000];
memset(szWork, 0, 20000);
INT nNum = 0,index = 0;
INT nLen = 0;


nNum = ::GetPrivateProfileSection(_T("RIZHI"), szBuf, 20000, m_strCurPathRiZhi);//獲得組名為“RIZHI”的鍵名鍵值的佔用位數
nLen = _tcslen(szBuf);        //長度
TCHAR* p = szBuf;
while (nLen < nNum)        //獲得的行數範圍
{
_tcscpy(szWork, p);    //此處的szWork為所需內容 鍵名和鍵值
nLen = nLen + 1;
p = &szBuf[nLen];
nLen += _tcslen(p);
str.Format("%d", index);                     //得到的第幾行給字串str
m_rizhilist.InsertItem(index, str);        //輸出到表中顯示
str.Format(szWork);                            //得到的鍵名和鍵值給字串str
m_rizhilist.SetItemText(index, 1, str);  //輸出到表中顯示
index++;                                             //下一行。。
}

//例:

//寫入.INI

void DAN_1::LED_WriteStatic(CString VALUE)        //寫入第RAD組的第RADLed個的燈的狀態值=Value;
{
CString str;
str.Format("RAD%d",RadLed);
switch (RAD)
{
case 1:WritePrivateProfileString("RAD_LED_ONE", str, VALUE, m_strRadLedDan);  break;
case 2:WritePrivateProfileString("RAD_LED_TWO", str, VALUE, m_strRadLedDan); break;
case 3:WritePrivateProfileString("RAD_LED_THREE", str, VALUE, m_strRadLedDan); break;
case 4:WritePrivateProfileString("RAD_LED_FOUR", str, VALUE,  m_strRadLedDan); break;
case 5:WritePrivateProfileString("RAD_LED_FIVE", str, VALUE,  m_strRadLedDan); break;
case 6:WritePrivateProfileString("RAD_LED_SIX", str, VALUE, m_strRadLedDan); break;
case 7:WritePrivateProfileString("RAD_LED_SEVEN", str, VALUE, m_strRadLedDan); break;
case 8:WritePrivateProfileString("RAD_LED_EIGHT", str, VALUE, m_strRadLedDan); break;
case 9:WritePrivateProfileString("RAD_LED_NINE", str, VALUE,  m_strRadLedDan); break;
case 10:WritePrivateProfileString("RAD_LED_TEN", str, VALUE,  m_strRadLedDan); break;
}
}

//讀出.INI

void DAN_1::LED_Static(void)        //讀第RAD組的100個LED的狀態訊號記憶顯示
{
GetModuleFileName(NULL, m_strRadLedDan.GetBuffer(MAX_PATH), MAX_PATH);
m_strRadLedDan.ReleaseBuffer();
m_strRadLedDan = m_strRadLedDan.Left(m_strRadLedDan.ReverseFind('\\') + 1);
m_strRadLedDan += "RadLed.ini";
CString str,strV;//獲得組的鍵名  RAD1--RAD100
CString GROUP;//獲得組名ONE -- TEN


switch (RAD)
{
case 1:GROUP = "RAD_LED_ONE";break;
case 2:GROUP = "RAD_LED_TWO";break;
case 3:GROUP = "RAD_LED_THREE";break;
case 4:GROUP = "RAD_LED_FOUR";break;
case 5:GROUP = "RAD_LED_FIVE";break;
case 6:GROUP = "RAD_LED_SIX";break;
case 7:GROUP = "RAD_LED_SEVEN";    break;
case 8:GROUP = "RAD_LED_EIGHT";break;
case 9:GROUP = "RAD_LED_NINE";break;
case 10:GROUP = "RAD_LED_TEN";break;
}


for (int i = 1; i <= 100;i++)
{
str.Format("RAD%d",i);
GetPrivateProfileString(GROUP, str, NULL, strV.GetBuffer(20), 20, m_strRadLedDan);
strV.ReleaseBuffer();
if (strV == "bb")m_value = m_hIconLedGreed; else if (strV == "cc") m_value = m_hIconLedYellow;

else if (strV == "dd") m_value = m_hIconLedBlue; else if (strV == "ee") m_value = m_hIconLedRed;

                 else m_value = m_hIconLedGray;

switch (i)
{
case 1:m_led1.SetIcon(m_value);break;case 2:m_led2.SetIcon(m_value);break;case 3:m_led3.SetIcon(m_value);break;case 4:m_led4.SetIcon(m_value);break;case 5:m_led5.SetIcon(m_value);break;
case 6:m_led6.SetIcon(m_value);break;case 7:m_led7.SetIcon(m_value);break;case 8:m_led8.SetIcon(m_value);break;case 9:m_led9.SetIcon(m_value);break;case 10:m_led10.SetIcon(m_value);break;
case 11:m_led11.SetIcon(m_value);break;case 12:m_led12.SetIcon(m_value);break;case 13:m_led13.SetIcon(m_value);break;case 14:m_led14.SetIcon(m_value);break;case 15:m_led15.SetIcon(m_value);break;
case 16:m_led16.SetIcon(m_value);break;case 17:m_led17.SetIcon(m_value);break;case 18:m_led18.SetIcon(m_value);break;case 19:m_led19.SetIcon(m_value);break;case 20:m_led20.SetIcon(m_value);break;
case 21:m_led21.SetIcon(m_value);break;case 22:m_led22.SetIcon(m_value);break;case 23:m_led23.SetIcon(m_value);break;case 24:m_led24.SetIcon(m_value);break;case 25:m_led25.SetIcon(m_value);break;
case 26:m_led26.SetIcon(m_value);break;case 27:m_led27.SetIcon(m_value);break;case 28:m_led28.SetIcon(m_value);break;case 29:m_led29.SetIcon(m_value);break;case 30:m_led30.SetIcon(m_value);break;
case 31:m_led31.SetIcon(m_value);break;case 32:m_led32.SetIcon(m_value);break;case 33:m_led33.SetIcon(m_value);break;case 34:m_led34.SetIcon(m_value);break;case 35:m_led35.SetIcon(m_value);break;
case 36:m_led36.SetIcon(m_value);break;case 37:m_led37.SetIcon(m_value);break;case 38:m_led38.SetIcon(m_value);break;case 39:m_led39.SetIcon(m_value);break;case 40:m_led40.SetIcon(m_value);break;
case 41:m_led41.SetIcon(m_value);break;case 42:m_led42.SetIcon(m_value);break;case 43:m_led43.SetIcon(m_value);break;case 44:m_led44.SetIcon(m_value);break;case 45:m_led45.SetIcon(m_value);break;
case 46:m_led46.SetIcon(m_value);break;case 47:m_led47.SetIcon(m_value);break;case 48:m_led48.SetIcon(m_value);break;case 49:m_led49.SetIcon(m_value);break;case 50:m_led50.SetIcon(m_value);break;

case 51:m_led51.SetIcon(m_value);break;case 52:m_led52.SetIcon(m_value);break;case 53:m_led53.SetIcon(m_value);break;case 54:m_led54.SetIcon(m_value);break;case 55:m_led55.SetIcon(m_value);break;
case 56:m_led56.SetIcon(m_value);break;case 57:m_led57.SetIcon(m_value);break;case 58:m_led58.SetIcon(m_value);break;case 59:m_led59.SetIcon(m_value);break;case 60:m_led60.SetIcon(m_value);break;
case 61:m_led61.SetIcon(m_value);break;case 62:m_led62.SetIcon(m_value);break;case 63:m_led63.SetIcon(m_value);break;case 64:m_led64.SetIcon(m_value);break;case 65:m_led65.SetIcon(m_value);break;
case 66:m_led66.SetIcon(m_value);break;case 67:m_led67.SetIcon(m_value);break;case 68:m_led68.SetIcon(m_value);break;case 69:m_led69.SetIcon(m_value);break;case 70:m_led70.SetIcon(m_value);break;
case 71:m_led71.SetIcon(m_value);break;case 72:m_led72.SetIcon(m_value);break;case 73:m_led73.SetIcon(m_value);break;case 74:m_led74.SetIcon(m_value);break;case 75:m_led75.SetIcon(m_value);break;
case 76:m_led76.SetIcon(m_value);break;case 77:m_led77.SetIcon(m_value);break;case 78:m_led78.SetIcon(m_value);break;case 79:m_led79.SetIcon(m_value);break;case 80:m_led80.SetIcon(m_value);break;
case 81:m_led81.SetIcon(m_value);break;case 82:m_led82.SetIcon(m_value);break;case 83:m_led83.SetIcon(m_value);break;case 84:m_led84.SetIcon(m_value);break;case 85:m_led85.SetIcon(m_value);break;
case 86:m_led86.SetIcon(m_value);break;case 87:m_led87.SetIcon(m_value);break;case 88:m_led88.SetIcon(m_value);break;case 89:m_led89.SetIcon(m_value);break;case 90:m_led90.SetIcon(m_value);break;
case 91:m_led91.SetIcon(m_value);break;case 92:m_led92.SetIcon(m_value);break;case 93:m_led93.SetIcon(m_value);break;case 94:m_led94.SetIcon(m_value);break;case 95:m_led95.SetIcon(m_value);break;
case 96:m_led96.SetIcon(m_value);break;case 97:m_led97.SetIcon(m_value);break;case 98:m_led98.SetIcon(m_value);break;case 99:m_led99.SetIcon(m_value);break;case 100:m_led100.SetIcon(m_value);break;
}
}
}