vc獲取程式的工作路徑的方法
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;
}
方法3
CFileDialog dlg(TRUE)
CFileDialog dlg(TRUE); // 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);
}
特別注意::以上3種方法獲取的路徑是當前的工作路徑,如果是A程式中打開了B程式,B程式中使用了上述方法或者".\\"的預設路徑的話得到的都是A的工作路徑下。