要求 打开程序同目录下同名的文件夹
如 G:\autorun.exe 运行后打开 G:\autorun 目录
PS 需要判断该目录是否存在
bool FolderIsExist(CString sPath)
{
WIN32_FIND_DATA wfd;
bool bValue = false;
HANDLE hFile = FindFirstFile(sPath, &wfd);
if ((hFile != INVALID_HANDLE_VALUE) && (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
bValue = true;
}
FindClose(hFile);
return bValue;
}
void OpenDir()
{
char appPath[MAX_PATH];
//得到自身文件路径
GetModuleFileName(NULL,appPath,256);
CString appPathString = appPath;
if (1) //这里怎么写?
{
char openDir[80]={0};
strcpy(openDir,"explorer ");
strcat(openDir,appPathString.Left(sizeof(appPathString)-4)); //这里貌似有问题。。
//打开目录
WinExec(openDir,1);
}
}
团队回答为
#include <afxwin.h>
#include <windows.h>
#pragma comment(lib, "libcmt.lib")
bool FolderIsExist(CString sPath)
{
WIN32_FIND_DATA wfd;
bool bValue = false;
HANDLE hFile = FindFirstFile(sPath, &wfd);
if ((hFile != INVALID_HANDLE_VALUE) && (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
bValue = true;
}
FindClose(hFile);
return bValue;
}
void OpenDir()
{
char appPath[MAX_PATH];
//得到自身文件路径
GetModuleFileName(NULL,appPath,256);
CString appPathString = appPath;
int i = appPathString.ReverseFind('\\');
CString path = appPathString.Left(i+1);
CString name = appPathString.Mid(i+1);
name = name.Left(name.Find('.'));
CString fullPath = path+name;
// puts(fullPath);
if (FolderIsExist(fullPath)) //这里怎么写?
{
char openDir[80]={0};
strcpy(openDir,"explorer ");
strcat(openDir, fullPath); //这里貌似有问题。。
WinExec(openDir, 1);
} else puts("NOT EXIST.");
}
int main()
{
OpenDir();
}
BUG为VC6.0.exe并不能打开文件夹VC6.0,点以及0被舍掉,忘解决