怎样获得可执行文件所在的当前路径
static char BASED_CODE szFilter[] = "SAVE FILE (*.txt)|*.txt|All Files (*.*)|*.*||";
CFileDialog dlg(TRUE,NULL,"cy.txt",OFN_HIDEREADONLY|
OFN_OVERWRITEPROMPT,szFilter,NULL);
CString m_save;
if(dlg.DoModal()==IDOK)
{
m_Save=dlg.GetPathName();
UpdateData(FALSE);
}
在上述程序中在m_save得到可执行文件的当前路径,但在VC下直接运行时得到的是
上一级命令,直接运行生成的可执行文件时却得到"C:\我的文档",请问如何直接运行生成的可执行文件时得到可执行文件所在的当前路径
问题点数:80、回复次数:12Top
1 楼shilong(银羽 www.ylog.net)回复于 2002-06-21 16:10:56 得分 0
CString sIniFilePath;
CString sModFileName;
GetModuleFileName(NULL, sModFileName.GetBuffer(MAX_PATH), MAX_PATH);
sModFileName.ReleaseBuffer();
sModFileName.MakeReverse();
sIniFilePath = sModFileName.Right(sModFileName.GetLength() - ModFileName.Find('\\'));
sIniFilePath.MakeReverse();
//sIniFilePath就是你要得到的路径Top
2 楼lilachue(静水思雅)回复于 2002-06-21 16:11:19 得分 5
GetCurrentDirectory ()Top
3 楼papaya_stone(^_^)shentong(^_^)回复于 2002-06-21 16:11:30 得分 5
char sCurrentDirectour[256];
GetModuleFileName(AfxGetInstanceHandle(),sCurrentDirectory,sizeof(sCurrentDirectory));Top
4 楼PioneerMan(南蛮)回复于 2002-06-21 16:12:32 得分 5
GetModuleFileNameTop
5 楼shilong(银羽 www.ylog.net)回复于 2002-06-21 16:12:35 得分 30
CString sIniFilePath;
CString sModFileName;
GetModuleFileName(NULL, sModFileName.GetBuffer(MAX_PATH), MAX_PATH);
sModFileName.ReleaseBuffer();
sModFileName.MakeReverse();
sIniFilePath = sModFileName.Right(sModFileName.GetLength() - sModFileName.Find('\\'));
sIniFilePath.MakeReverse();
-----------------------------------
sIniFilePath就是你要得的路径
Top
6 楼jaidy(骑士)回复于 2002-06-21 16:12:59 得分 5
GetModuleFileName()Top
7 楼liqi(sniper)回复于 2002-06-21 16:20:07 得分 10
TCHAR exeFullPath[100];
GetModuleFileName(NULL,exeFullPath,100);Top
8 楼superspider(蜘蛛侠)回复于 2002-06-21 16:20:55 得分 0
这样可以得到当前路径,但我想在dlg.DoModal()后弹出的对话框时显示时的路径也为当前路径,如要存盘时显示的对话框,应该有个dlg的成员函数还是属性Top
9 楼wistaria(听风听雨)回复于 2002-06-21 16:35:15 得分 10
为dlg加一个成员变量.Top
10 楼superspider(蜘蛛侠)回复于 2002-06-21 16:40:24 得分 0
什么成员变量,大哥,可不可讲的详细点啊Top
11 楼nuaawyd(血影狂刀)回复于 2002-06-21 16:50:49 得分 10
GetModuleFileName
The GetModuleFileName function retrieves the full path and filename for the executable file containing the specified module.
Windows 95: The GetModuleFilename function will return long filenames when an application's version number is greater than or equal to 4.00 and the long filename is available. Otherwise, it returns only 8.3 format filenames.
DWORD GetModuleFileName(
HMODULE hModule, // handle to module to find filename for
LPTSTR lpFilename, // pointer to buffer to receive module path
DWORD nSize // size of buffer, in characters
);
Parameters
hModule
Handle to the module whose executable filename is being requested. If this parameter is NULL, GetModuleFileName returns the path for the file used to create the calling process.
lpFilename
Pointer to a buffer that is filled in with the path and filename of the given module.
nSize
Specifies the length, in characters, of the lpFilename buffer. If the length of the path and filename exceeds this limit, the string is truncated.
Top
12 楼Lemon_2000(柠檬)回复于 2002-06-21 18:03:28 得分 0
upTop




