VC獲取當前路徑及程序名的實現(xiàn)代碼
一、獲取當前運行目錄的絕對路徑
1、使用GetCurrentDirectory函數(shù)
假設程序路徑為D:\Test\tst.exe,執(zhí)行GetCurrentDirectory函數(shù)
char pBuf[MAX_PATH]; GetCurrentDirectory(MAX_PATH,pBuf);
pBuf="D:\Test"
但是如果使用CFileDialog、CFile::Open等函數(shù)后,設置不當則會導致再次獲取當前路徑值改變。所以,如要避免當前路徑改變,如果使用CFileDialog,則要把在CFileDialog的dwFlags標志設置為OFN_NOCHANGEDIR。如下:
CFileDialog hFileDlg(false,NULL , NULL, OFN_FILEMUSTEXIST | OFN_READONLY | OFN_PATHMUSTEXIST | OFN_NOCHANGEDIR, TEXT("Text Files (*.txt)|*.txt|All Files(*.*)|*.*|"), NULL);
也可以,先執(zhí)行GetCurrentDirectory把獲取到目錄路徑保存下來,處理完成后,再次SetCurrentDirectory設置一下。
2、使用GetModuleFileName
CString strCurPath; GetModuleFileName(NULL,strCurPath.GetBuffer(MAX_PATH),MAX_PATH); int pos= strCurPath.ReverseFind(_T('\\')); strCurPath = strCurPath.Left(pos);
輸出(路徑包括運行文件名):
strCurPath="D:\Test\tst.exe"
二、獲取打開文件的完整路徑
通過對話框打開文件時,一般均需獲取打開文件的完整路徑,可使用CFileDialog的GetPathName函數(shù),代碼如下:
CFileDialog hFileDlg(false,NULL , NULL, OFN_FILEMUSTEXIST | OFN_READONLY | OFN_PATHMUSTEXIST | OFN_NOCHANGEDIR, TEXT("Text Files (*.txt)|*.txt|All Files(*.*)|*.*|"), NULL); if(hFileDlg.DoModal() == IDOK) { m_strEdtSrcFile = hFileDlg.GetPathName(); UpdateData(FALSE); }
三、獲取當前運行程序名
通過GetModuleFileName獲取完整路徑后,可以使用PathStripPath API函數(shù)解析路徑,代碼示例如下:
/* #include "shlwapi.h" #pragma comment(lib, "shlwapi.lib ") */ TCHAR szPath2[] = TEXT("D:\\Test\\tst.exe"); PathStripPath(szPath2); // Result: szPath2 ==tst.exe TCHAR szPath3[] = TEXT("D:\\Test\\Debug"); PathStripPath(szPath3); // Result: szPath3 == Debug TCHAR szPath4[] = TEXT("D:\\Test\\Debug\\"); PathStripPath(szPath4); // Result: szPath4 == Debug\ TCHAR szPath5[] = TEXT("D:\\"); PathStripPath(szPath5); // Result: szPath5 == D:\
使用此函數(shù)要注意,并非只是提取文件名,如果它不能識別時,則會返回原始字符串(不做處理)。所以,不放心的話,自己手動提取文件名更為保險。
CString strCurPath; GetModuleFileName(NULL,strCurPath.GetBuffer(MAX_PATH),MAX_PATH) strCurPath.ReleaseBuffer();//Must ReleaseBuffer, or GetLength=0 int pos= strCurPath.ReverseFind(_T('\\')); int len = strCurPath.GetLength(); strCurPath = strCurPath.Right(len-pos-1);
參考資料:
http://msdn.microsoft.com/en-us/library/windows/desktop/bb773756%28v=vs.85%29.aspx
CString 詭異的 GetLength() 返回 0
如果CString是從GetPrivateProfileStr()返回的
記得return 該CString 前 釋放buffer
要不,CString.GetLength()將返回0
找了半天原因,
----------------------------------------------------
“If you use the pointer returned by GetBuffer to change the string contents, you must call ReleaseBuffer before using any other CString member functions. ”
在i=m_SZFileName.GetLength()之前調用m_SZFileName.ReleaseBuffer()就可以了。
臨時搞兩天VC,在VC里如何獲取當前程序的名字和路徑以及如何分割字符串
#include "stdafx.h" #include <windows.h> int main(int argc, char* argv[]) { //先獲取運行程序的完整路徑 char szFileName[256]; memset(szFileName,'"0',sizeof(szFileName)); GetModuleFileName(NULL,szFileName, sizeof(szFileName)); //再分割完整路徑的字符串,最后一個就是程序的名字 char seps[] = "\\" ; char *token = NULL; char exeName[256]; memset(exeName,'\0',sizeof(exeName)); token = strtok( szFileName, seps ); while( token != NULL ) { sprintf(exeName,"%s",token); token = strtok( NULL, seps ); } printf("%s\n",exeName); getchar(); return 0; }
相關文章
for循環(huán)中刪除map中的元素valgrind檢測提示error:Invalid read of size 8
這篇文章主要介紹了for循環(huán)中刪除map中的元素valgrind檢測提示error:Invalid read of size 8 的相關資料,需要的朋友可以參考下2016-07-07vc++實現(xiàn)的tcp socket客戶端和服務端示例
這篇文章主要介紹了vc++實現(xiàn)的tcp socket客戶端和服務端示例,需要的朋友可以參考下2014-03-03