mfc 对写ini文件

1、在对话框中添加Edit控件

2、在初始化对话框中添加代码如下:

//从文件中读取value

CString SName,filePath;
filePath = GetModuleDir(); //获取文件路径
filePath += "\\config.ini";
LPWSTR  lpstr = (LPWSTR)(LPCWSTR)SName; //将CString 转换成LPWSTR 
//获取字符串
GetPrivateProfileString(_T("Serial"),_T("Port"),_T(""),lpstr,20,filePath);
//((CEdit *)GetDlgItem(IDC_EDIT1))->SetWindowText(SName);


//获取数字
UINT baudRate = GetPrivateProfileInt(_T("Serial"),_T("BaudRate"),0,filePath);
CString temp;
temp.Format(_T("%d"),baudRate);
//temp = temp + SName ; //注意,转化的cString 不能用“+”重载
((CEdit *)GetDlgItem(IDC_EDIT1))->SetWindowText(temp);

其中GetModuleDir方法如下:

CString CIniFileDlg:: GetModuleDir()
{
 HMODULE module = GetModuleHandle(0);
 TCHAR  pFileName[100];
 GetModuleFileName(module, pFileName, 100);
 CString csFullPath(pFileName);
 int nPos = csFullPath.ReverseFind( _T('\\') );
 if( nPos < 0 )
return CString("");
 else
return csFullPath.Left( nPos );
}

3、往ini文件中写value

添加一个button控件,并双击添加响应单击事件并在相应函数中添加如下代码:

CString temp,filePath;
filePath = GetModuleDir();
filePath += "\\config.ini";

((CEdit *)GetDlgItem(IDC_EDIT1))->GetWindowText(temp);
WritePrivateProfileString(_T("Serial"),_T("BaudRate"),temp,filePath);

((CEdit *)GetDlgItem(IDC_EDIT2))->GetWindowText(temp);
WritePrivateProfileString(_T("Serial"),_T("Port"),temp,filePath);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

woquNOKIA

谢谢老板!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值