GetCurrentDirectory就可以 GetCurrentDirectory The GetCurrentDirectory function retrieves the current directory for the current process. DWORD GetCurrentDirectory( DWORD nBufferLength, // size of directory buffer LPTSTR lpBuffer // directory buffer );在你程序里char dir[128]; 然后GetCurrentDirectory(128,dir),就可以
我觉得还是这个函数保险:DWORD GetModuleFileName( HMODULE hModule, // handle to module LPTSTR lpFilename, // path buffer DWORD nSize // size of buffer ); Parameters hModule [in] Handle to the module whose path is being requested. If this parameter is NULL, GetModuleFileName retrieves the path for the current module. lpFilename [out] Pointer to a buffer that receives the fully-qualified path for the module. If the length of the string exceeds the size specified by the nSize parameter, the string is truncated. Windows NT/2000/XP: The path can have the prefix "\\?\", depending on how the module was loaded. For more information, see File Name Conventions. nSize [in] Specifies the length of the lpFilename buffer, in TCHARs.
GetCurrentDirectory(255,str);
strcat(str,"\\myfile");
GetCurrentDirectory
The GetCurrentDirectory function retrieves the current directory for the current process. DWORD GetCurrentDirectory(
DWORD nBufferLength, // size of directory buffer
LPTSTR lpBuffer // directory buffer
);在你程序里char dir[128];
然后GetCurrentDirectory(128,dir),就可以
GetModuleFileName(NULL,szFile,256);
::MessageBox(hWnd,szFile,"路径",MB_OK);
HMODULE hModule, // handle to module
LPTSTR lpFilename, // path buffer
DWORD nSize // size of buffer
);
Parameters
hModule
[in] Handle to the module whose path is being requested. If this parameter is NULL, GetModuleFileName retrieves the path for the current module.
lpFilename
[out] Pointer to a buffer that receives the fully-qualified path for the module. If the length of the string exceeds the size specified by the nSize parameter, the string is truncated.
Windows NT/2000/XP: The path can have the prefix "\\?\", depending on how the module was loaded. For more information, see File Name Conventions. nSize
[in] Specifies the length of the lpFilename buffer, in TCHARs.
GetModuleFileName(NULL, chDir, 1024);
CString strAppPath = chDir;//路徑和APP名
strAppPath= strAppPath .Left(strAppPath.ReverseFind(_T('\\'))1);//路徑
{
TCHAR tszTemp[2000];
int nTemp;
HMODULE hSelf;
hSelf = NULL;
if (!strPathNoDirectory.IsEmpty ())
hSelf = ::GetModuleHandle (strPathNoDirectory); nTemp = ::GetModuleFileName (hSelf, tszTemp, sizeof(tszTemp)/sizeof(*tszTemp));
if (0 == nTemp)
return false; ASSERT (nTemp < sizeof(tszTemp)/sizeof(*tszTemp));
tszTemp[nTemp] = _T('\0');
strPathCompleted = tszTemp;
return true;
}
bool GetExecutable (CString &strPathCompleted, CString strPathNoDirectory = _T(""));
如果你是把函数放在DLL里的话,就必须指定strPathNoDirectory(形式如:_T("MSDEV.EXE")),如果函数不在DLL里可以不必指定该参数
用GetCurrentDirectory設置會有問題得.比如說你在打開一個
CFileDialog d.Open(....);
d.Domodal();//假如你在這裡做了路徑選擇以後
你會發現只要你在程序任何地方緊接掉用GetCurrentDirectory..他得到得是
CFileDialog所選擇後得路徑..這並不是你想要的.