您的位置:首页 > 编程语言 > C语言/C++

c++ winapi 获取当前程序/工程所在路径

2016-01-12 15:29 603 查看
http://blog.sina.com.cn/s/blog_6294abe701013ick.html

1.方法1

   char pBuf[MAX_PATH];                                               //存放路径的变量

   GetCurrentDirectory(MAX_PATH,pBuf);                   //获取程序的当前目录

   strcat(pBuf,"//");

   strcat(pBuf,AfxGetApp()->m_pszExeName);   

   strcat(pBuf,".exe");                                                       //获取程序的全文件名

---------------------------------------------------------------------------------------------------------------------------------------------

2.方法2

   //函数返回应用程序所在的路径   

   CString    CClientApp::ReturnPath()   

   {   

   CString    sPath;   

   GetModuleFileName(NULL,sPath.GetBufferSetLength(MAX_PATH+1),MAX_PATH);   

   sPath.ReleaseBuffer    ();   

   int    nPos;   

   nPos=sPath.ReverseFind('//');   

   sPath=sPath.Left(nPos);   

   return    sPath;   

   }



TCHAR szFilePath[MAX_PATH + 1];
GetModuleFileName(NULL, szFilePath, MAX_PATH);
(_tcsrchr(szFilePath, _T('\\')))[1] = 0; //删除文件名,只获得路径
CString str_url = szFilePath; //str_url 中保存的是当前目录

---------------------------------------------------------------------------------------------------------------------------------------------

3.方法3

CFileDialog dlg(TRUE)

 

CFileDialog dlg(TRUE);//<-这里用TRUE与FALSE有什么不同?

     // TRUE是“打开”对话框

     // FALSE是“另存为”对话框

int ret=dlg.DoModal();

if(ret==IDOK)

{

CString pathname=dlg.GetPathName();  //得到文件所在路径+文件名

CString filename=dlg.GetFileName(); //得到文件名

char tbuf[120];

sprintf(tbuf,"The %s file in %s is saved!",filename,pathname);

AfxMessageBox(tbuf);
}

---------------------------------------------------------------------------------------------------------------------------------------------

1.GetCurrentDirectory ,由于打开,保存文件对话框都会改变当前路径,一般不建议使用这个

2.GetModuleFileName  头文件 Header: Declared in Winbase.h; include Windows.h.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  windows