首先默认的是当前目录,然后是系统目录、Windows目录
差异在于少了指定文件,指定文件在CWinApp::m_pszProfileName中定义。并且少了指定长度。1。CWinApp::GetProfileString 
GetProfileString( LPCTSTR lpszSection, LPCTSTR lpszEntry, LPCTSTR lpszDefault = NULL );
2。DWORD GetProfileString(
  LPCTSTR lpAppName,        // address of section name
  LPCTSTR lpKeyName,        // address of key name
  LPCTSTR lpDefault,        // address of default string
  LPTSTR lpReturnedString,  // address of destination buffer
  DWORD nSize               // size of destination buffer
);

解决方案 »

  1.   

    我用
    CWinApp* pApp = AfxGetApp();
    CString strValue;
    strValue=pApp->GetProfileString(_T("Network"), _T("UDP_IP"), _T("192.168.1.9"));
    怎么总是取到默认的值,而取不到配置文件中的值?
      

  2.   

    pApp->m_pszProfileName="FILENAME.INI";
    strValue=pApp->GetProfileString(_T("Network"), _T("UDP_IP"), _T("192.168.1.9"));
      

  3.   

    首先你應該在 InitInstace 中把SetRegistryKey(_T("Local AppWizard-Generated Applications"));//use the registry注釋掉,然後加上          //add this modify the direct of my.ini
    char path[256];
    memset(path,0,sizeof(path));
    GetModuleFileName(NULL,path,255);
    CString strPath = path;
    int index = strPath.ReverseFind('\\');
    strPath = strPath.Left(index);
    memset(path,0,sizeof(path));
    strcpy(path,strPath);
    free((void*)theApp.m_pszProfileName);
    if(path[0] == 0)
    theApp.m_pszProfileName = _tcsdup(_T("C:\\my.ini"));
    else
    {
    strcat((char *)path,_T("\\my.ini"));
    theApp.m_pszProfileName = _tcsdup((const char *)path);
    }
    這樣就設置了 ini 的位置
    那兩個函數的區別,與普通的 sdk 函數與 mfc 包裝過的函數的區別一致
      

  4.   

    首先把SetRegistryKey(_T("Local AppWizard-Generated Applications"));注釋掉,然後加上
              //add this modify the direct of my.ini
    char path[256];
    memset(path,0,sizeof(path));
    GetModuleFileName(NULL,path,255);
    CString strPath = path;
    int index = strPath.ReverseFind('\\');
    strPath = strPath.Left(index);
    memset(path,0,sizeof(path));
    strcpy(path,strPath);
    free((void*)theApp.m_pszProfileName);
    if(path[0] == 0)
    theApp.m_pszProfileName = _tcsdup(_T("C:\\my.ini"));
    else
    {
    strcat((char *)path,_T("\\my.ini"));
    theApp.m_pszProfileName = _tcsdup((const char *)path);
    }
    就把該 my.ini 設置到與 exe 同一目錄了
      

  5.   

    SetRegistryKey(_T("Local AppWizard-Generated Applications"));
    注釋了嗎?不注釋是讀註冊表
      

  6.   

    直接用API读取,就不会出现这样的混淆了
      

  7.   

    //SetRegistryKey(_T("Local AppWizard-Generated Applications"));
    free((void*)theApp.m_pszProfileName);
    theApp.m_pszProfileName = _tcsdup(_T("C:\\windows\\win.ini"));
    CString strValue;
    strValue = theApp.GetProfileString(_T("windows"), _T("run"), _T("failed"));
    測試成功
      

  8.   

    可以了,谢谢!
    请问gameboy999(我心迷茫),直接用API读取要怎么读?
      

  9.   

    如果不把SetRegistryKey(_T("Local AppWizard-Generated Applications"))注释掉,
    配置文件要放在哪里才能让程序读取到?
      

  10.   

    如果不把SetRegistryKey(_T("Local AppWizard-Generated Applications"))注释掉,
    讀寫的是註冊表,不會有配置文件,
    別忘了給分
      

  11.   

    一定给!可是我把这段代码放在dll中,编译时出现下面错误:
    error C2660: 'GetProfileStringA' : function does not take 3 parameters
    这是怎么回事?
      

  12.   

    如果你要在 dll 中使用,你必須選擇可以使用 mfc 類庫的 dll 
    然後把
    //SetRegistryKey(_T("Local AppWizard-Generated Applications"));
    free((void*)theApp.m_pszProfileName);
    theApp.m_pszProfileName = _tcsdup(_T("C:\\windows\\win.ini"));加在 initinstance 中
    在 dll 中WORD GetPStr_GU(CString &returnstr)
    {
    CWinApp * pApp = AfxGetApp();
    returnstr = pApp->GetProfileString(("windows"), ("run"), ("failed"));
    return returnstr.GetLength();
    }另外,::GetProfileString()
    ::GetProfileString(("Mail"), ("CMCDLLNAME"), ("failed"),
    (LPTSTR)returnstr,255);
    只能讀取標準的 Win.ini ,只能讀取該文件,可以在不能使用mfc類庫的dll中使用
      

  13.   

    这么说是不是在DLL中配置文件只能从WIN.INI中读取?? :(
      

  14.   

    不是,在可以使用 mfc 類庫的 dll 中可以讀取 call 該 dll 的程序
    指定的 ini 文件只要 dll 中可以使用 CWinApp 就可以了
      

  15.   

    可是我在dll中用以下代码无法取得配置文件中的值char path[256];
    memset(path,0,sizeof(path));
    GetModuleFileName(NULL,path,256);
    ::lstrcat(path, _T("\\getini.ini"));
    char buffer[256];
    unsigned long length;free((void*)theApp.m_pszProfileName);
    theApp.m_pszProfileName = _tcsdup((const char *)path);
    length=GetProfileString(_T("Network"),_T("UDP_IP"),_T("192.168.1.255"),buffer,256);
      

  16.   

    char path[256];
    memset(path,0,sizeof(path));
    GetModuleFileName(NULL,path,256);
    ::lstrcat(path, _T("\\getini.ini"));
    char buffer[256];
    unsigned long length;free((void*)theApp.m_pszProfileName);
    theApp.m_pszProfileName = _tcsdup((const char *)path);應該放在 主程序 中,在 dll 中這樣用CWinApp * pApp = AfxGetApp();
    returnstr = pApp->GetProfileString(("windows"), ("run"), ("failed"));如果你的 dll 中不可以使用 CWinApp 則,無法讀取指定文件.
    ;;GetProfileString(_T("Network"),_T("UDP_IP"),_T("192.168.1.255"),buffer,256);
    使用 5 個參數,只能讀取 win.ini
      

  17.   

    大虾,我用下面代码在dll中读的还是默认值啊!!char path[256];
    memset(path,0,sizeof(path));
    GetCurrentDirectory(256,path);
    ::lstrcat(path, _T("\\getini.ini"));free((void*)theApp.m_pszProfileName);
    theApp.m_pszProfileName = _tcsdup((const char *)path);
    CWinApp * pApp = AfxGetApp();
    CString returnstr =pApp->GetProfileString(("Network"), ("UDP_IP"), ("192.168.1.255"));
      

  18.   

    大虾,我的email在上面,先谢了。
    回头就给分
      

  19.   

    大哥,读的怎么还是默认值啊?AFX_MANAGE_STATE(AfxGetStaticModuleState());
    free((void*)theApp.m_pszProfileName);
    theApp.m_pszProfileName = _tcsdup(_T("C:\\windows\\win.ini"));
    CString returnstr=theApp.GetProfileString(("Network"),("UDP_IP"), ("failed"));
    int a = returnstr.GetLength() ;win.ini中有如下字段:
    [Network]
    [UDP_IP]=192.168.1.255
    可是读的结果是“failed”。
    是不是我的机子中邪了?? :(
      

  20.   

    不会吧,那个程序在我这很好啊,可以读任意的 ini 文件啊
      

  21.   

    为什么win.ini中原有的字段可以读得出来,而我加入的
    [Network]
    UDP=1.1.1.1
    却读不出来????
      

  22.   

    你用 WriteProfileString 写入的数据才可以
      

  23.   

    GetPrivateProfileString(...) //读,最后一个参数是配置文件的绝对路径
    WritePrivateProfileString(...) //写,看MSDN
      

  24.   

    现在可以了,谢谢clack(千里快哉风),Gu_c_h(Gu)
    给分!!