方法一:
TCHAR tcsModulePath[_MAX_PATH];
::GetModuleFileName(NULL, tcsModulePath, _MAX_PATH);
char drive[_MAX_DRIVE];
char dir[_MAX_DIR];
_splitpath(tcsModulePath, drive, dir, NULL, NULL);
CString strPath;
strPath.Format("%s%s从控端.exe", drive, dir); //strPath即为得到的当前运行程序所在目录
//AfxMessageBox(strPath);
//Unicode:
CString m_strPath;
int nPos;
GetModuleFileName(NULL, m_strPath.GetBufferSetLength(MAX_PATH + 1), MAX_PATH);
m_strPath.ReleaseBuffer();
nPos = m_strPath.ReverseFind(_T('\\'));
m_strPath = m_strPath.Left(nPos);
AfxMessageBox(m_strPath);
方法二:(验证过)
CString m_TempFile;
//获取应用程序的全路径
char exeFullPath[MAX_PATH];
GetModuleFileName(NULL, exeFullPath, MAX_PATH);
//将其格式化为字符串
m_TempFile.Format(_T("%s"), exeFullPath);
//去掉应用程序的全名(15为应用程序文件全名的长度)
exeFullPath[m_TempFile.GetLength()-15]='\0';
//得到应用程序所在路径
m_TempFile.Format(_T("%s"), exeFullPath);
//得到程序中文件的全路径
m_TempFile+="xxx.exe";
2.查找本地目标文件时候存在
PathFileExists 函数可以用来查看一个文件或文件夹是否存在。
用法:
在头文件中
#include <shlwapi.h>
#pragma comment(lib,"Shlwapi.lib") //如果没有这行,会出现link错误
if (PathFileExists(strDBPath))
{
//存在
}
else //不存在
3.删除指定路径下的文件
DeleteFile(file_path);