先要进行文件操作,要获得当前目录,请问各位大哥谁实现过?
解决方案 »
- 有两个小问题请各位高手指教,急急!!
- 虚拟键盘输入程序
- 100分!如何在程序中用POST方法自动登陆JSP网站
- 使用sendmessage的WM_COPYDATA方式交互delphi app 和win32 app的错误机制如何处理?详细见题
- 急!!!!!!!!如何创建一个ocx控件实现既定程序的功能?(在线等候)
- 请问高人:我要做个小球做平面运动的动画,可以通过输入控制他的方向,运动速度等.该如何实现呢?
- 下面的程序编译时报错(odl),谢谢高手解答:)
- 请教:建立了TCP连接后,服务器端怎样取得客户端的端口号?
- 工具条导入的问题(很弱智,不要见笑)
- 大神求帮助。。
- 运行时出现的错误,不明白,请赐教
- 今天下午的CET六级考得不好,估计不过,心情不好,散分……
The GetCurrentDirectory function retrieves the current directory for the current process. DWORD GetCurrentDirectory(
DWORD nBufferLength, // size, in characters, of directory buffer
LPTSTR lpBuffer // pointer to buffer for current directory
);
Parameters
nBufferLength
Specifies the length, in characters, of the buffer for the current directory string. The buffer length must include room for a terminating null character.
lpBuffer
Pointer to the buffer for the current directory string. This null-terminated string specifies the absolute path to the current directory.
Return Values
If the function succeeds, the return value specifies the number of characters written to the buffer, not including the terminating null character. If the function fails, the return value is zero. To get extended error information, call GetLastError. If the buffer pointed to by lpBuffer is not large enough, the return value specifies the required size of the buffer, including the number of bytes necessary for a terminating null character.
GetModuleFileName(NULL,Path.GetBufferSetLength(MAX_PATH+1),MAX_PATH);//得到文件绝对路径
DWORD GetCurrentDirectory(
DWORD nBufferLength, // size, in characters, of directory buffer
LPTSTR lpBuffer // pointer to buffer for current directory
);当前目录
DWORD GetCurrentDirectory(
DWORD nBufferLength, // size, in characters, of directory buffer
LPTSTR lpBuffer // pointer to buffer for current directory
);char szFileDir[120];
GetCurrentDirectory(120,szFileDir);
如对话框在OnInitDialog下
SDI在OnInitialUpdate下
GetModuleFileName(NULL,Path.GetBufferSetLength(MAX_PATH+1),MAX_PATH);
上面是取得当前可执行文件的绝对路径文件名.
GetModuleFileName(NULL,sPath.GetBufferSetLength (MAX_PATH+1),MAX_PATH);
sPath.ReleaseBuffer ();
int nPos;
nPos=sPath.ReverseFind (_T('\\'));
sPath=sPath.Left (nPos);
lpBuffer
Pointer to the buffer for the current directory string. This null-terminated string specifies the absolute path to the current directory.
这句话难道不是说这个当前目录是绝对路径吗?
CString CXXXXXXApp::GetCurDir()
{
TCHAR sDrive[_MAX_DRIVE];
TCHAR sDir[_MAX_DIR];
TCHAR sFilename[_MAX_FNAME],Filename[_MAX_FNAME];
TCHAR sExt[_MAX_EXT];
GetModuleFileName(AfxGetInstanceHandle(), Filename, _MAX_PATH);
_tsplitpath(Filename, sDrive, sDir, sFilename, sExt);
CString homeDir(CString(sDrive) + CString(sDir));
int nLen = homeDir.GetLength();
if(homeDir.GetAt(nLen-1) != _T('\\'))
homeDir += _T('\\');
return homeDir;
}
函数是可行的,稳定的。
GetModuleFileNameA才能获得程序所在的目录。
G:\开发目录\readfile\Release\readfile.exe //GetModuleFileNameA所得路径,但包含本程序文件名然而, 楼主希望:如何获得程序运行的当前目录,并非程序文件名和当前目录
GetCurrentDirectory();