请问如何获得程序运行所在路径? 
MFC 方法有吗?

解决方案 »

  1.   


    #include <stdlib.h>
    void main( void )
    {
       printf("The full path of the executing program is : %Fs\n", _pgmptr);
    }
      

  2.   

    在非CONCOLE方式的MFC程序里也可以用
      

  3.   

    char chPath[MAX_PATH];
    GetModuleFileName(NULL,chPath,MAX_PATH);//得到执行文件名
    CString strPath;
    strPath = strPath.ReverseFind('\\'); 
    //strPath就是你的应用所在目录了
      

  4.   

    GetModuleFileName()和GetCurrentDirectory()都可以!
      

  5.   

    GetCurrentDirectory()不准确!!!比如你的MFC程序在c:\test,你开始使用GetCurrentDirectory()可以得到准确的目录,假如你的程序你使用了CFileDialog来做了选择文件的操作,那么再用GetCurrentDirectory()得到的将是你用CFileDialog新选择的目录
      

  6.   

    可以用这段代码来测试一下void CDirDlg::OnButton1() 
    {
    // TODO: Add your control notification handler code here
    char path[1024];
    ZeroMemory(path,1024);
    GetCurrentDirectory(1024,path);
    ::MessageBox(NULL,path,"test",MB_ICONINFORMATION); CFileDialog mFileDlg(TRUE, NULL,NULL, OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT|OFN_ALLOWMULTISELECT,"All Files (*.*)|*.*| |", AfxGetMainWnd()); 
    if (mFileDlg.DoModal()==IDOK)
    {
    UpdateData(FALSE);
    }
    ZeroMemory(path,1024);
    GetCurrentDirectory(1024,path);
    ::MessageBox(NULL,path,"test",MB_ICONINFORMATION);
    }
    记得选择文件的时候要选择一个和当前目录不同的文件
    然后就可以看到GetCurrentDirectory()得到的目录不准确了个人觉得用_pgmptr最方便了
    可以用_splitpath来分解目录
    可以满足任何需要