|
文件目录操作 1.创建目录(API) BOOL CreateDirectory(LPCTSTR pstrDirName);//pstrDirName是全路径 2.删除目录(API) BOOL RemoveDirectory( LPCTSTR lpPathName ); 3.判断目录是否存在(Shell Function) #include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")
if (PathIsDirectory(_T("d:\\temp")))
AfxMessageBox(_T("存在"));
else
AfxMessageBox(_T("不存在"));
4.取得当前目录(API) DWORD GetCurrentDirectory( DWORD nBufferLength, LPTSTR lpBuffer ); 5.取得执行文件所在目录(API) DWORD GetModuleFileName( HMODULE hModule, LPTSTR lpFilename, DWORD nSize ); 6.取得功能目录(Shell Function) BOOL SHGetSpecialFolderPath( HWND hwndOwner, LPTSTR lpszPath, int nFolder, BOOL fCreate); 例:读取我的档案目录 TCHAR szDirFile[1024];
memset(szDirFile, 0, sizeof(szDirFile));
BOOL bRet = SHGetSpecialFolderPath(NULL,szDirFile,CSIDL_PERSONAL,true);
if (bRet)
{
AfxMessageBox(szDirFile);
}7.选择目录用的对话框界面 利用Shell Function可以打出选择目录用的对话框界面
#include<shlobj.h>
INT CALLBACK _BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM pData)
{
TCHAR szDir[MAX_PATH];
switch(uMsg)
{
case BFFM_INITIALIZED:
// WParam is TRUE since you are passing a path.
// It would be FALSE if you were passing a pidl.
SendMessage(hwnd, BFFM_SETSELECTION, TRUE, (LPARAM)pData);
break;
case BFFM_SELCHANGED:
// Set the status window to the currently selected path.
if (SHGetPathFromIDList((LPITEMIDLIST)lParam ,szDir))
{
SendMessage(hwnd,BFFM_SETSTATUSTEXT,0,(LPARAM)szDir);
}
break;
}
return 0;
}
CString GetFolderFullpath(LPCTSTR lpszDefault)
{
TCHAR buffDisplayName[MAX_PATH];
TCHAR fullpath[MAX_PATH];
BROWSEINFO browseinfo;
LPITEMIDLIST lpitemidlist;
ZeroMemory(&browseinfo, sizeof( BROWSEINFO ));
browseinfo.pszDisplayName = buffDisplayName ;
browseinfo.lpszTitle = _T("请选择目录");
browseinfo.ulFlags = BIF_RETURNONLYFSDIRS;
browseinfo.lParam = (LPARAM)lpszDefault;
browseinfo.lpfn = _BrowseCallbackProc;
if(!(lpitemidlist = SHBrowseForFolder(&browseinfo)))
{
AfxMessageBox(_T("没有选择目录"));
return CString(_T(""));
}
else
{
SHGetPathFromIDList(lpitemidlist, fullpath);
CoTaskMemFree(lpitemidlist);
return CString(fullpath);
}
}
void CTest77Dlg::OnBnClickedButton1()
{
CString strFolderFullpath = GetFolderFullpath(_T("d:\\Temp"));
if (strFolderFullpath != _T(""))
AfxMessageBox(strFolderFullpath);
}
|