windows开发杂记

一、判断文件或者文件夹是否存在。

if (!PathFileExists(csFilePath))
{::CreateDirectory(csFilePath, NULL);} 

二、打开windows类型对话框

static TCHAR strDirName[MAX_PATH];
BROWSEINFO bi;
CString szString = TEXT("选择一个文件夹");
bi.hwndOwner = ::GetFocus();
bi.pidlRoot = NULL;
bi.pszDisplayName = strDirName;
bi.lpszTitle = szString;
bi.ulFlags = BIF_BROWSEFORCOMPUTER | BIF_DONTGOBELOWDOMAIN | BIF_RETURNONLYFSDIRS;
bi.lpfn = NULL;
bi.lParam = 0;
bi.iImage = 0;
LPITEMIDLIST pItemIDList = ::SHBrowseForFolder(&bi);
if(pItemIDList == NULL)
{
	return ;
}
::SHGetPathFromIDList(pItemIDList, strDirName);

三、遍历目录下的所有文件

CString csFolder = csGPSDataPath + _T("\\*.txt");
CFileFind finder;
BOOL bWorking = finder.FindFile(csFolder);
while (bWorking)
{
	bWorking = finder.FindNextFile();
	if (finder.IsDots())
	{
		continue;
	}
	CString strPath = finder.GetFileName();
	CString strFile = csGPSDataPath + _T("\\") + strPath;
	vecAllFilePath.push_back(strFile);
}
finder.Close();

四、打开windows上次打开的对话框

BOOL CTestDlg::GetOpenSaveMRU(LPCTSTR strExtName,char *strMRUPath)
{
	HKEY hKEY;
	const char *mru="Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\ComDlg32\\OpenSaveMRU\\";
	CString strmru(mru);
	strmru += strExtName;
	long lRet = RegOpenKeyEx(HKEY_CURRENT_USER,strmru,0,KEY_READ,&hKEY);

	if (lRet != ERROR_SUCCESS ) {
		return FALSE;
	}

	DWORD keytype1 = REG_SZ;
	DWORD dwInBytes = 256;
	BYTE pInBuf[256];
	lRet = RegQueryValueEx(hKEY,"MRUList",NULL,&keytype1,pInBuf,&dwInBytes);
	if (lRet != ERROR_SUCCESS ) {
		return FALSE;
	}

	dwInBytes = 256;
	char strLast[] = {pInBuf[0],0};
	lRet = RegQueryValueEx(hKEY,(LPTSTR)strLast,NULL,&keytype1,(LPBYTE)   strMRUPath,&dwInBytes);

	if (lRet != ERROR_SUCCESS) {
		return FALSE;
	}

	CString str1(strMRUPath);
	int idx=str1.ReverseFind('\\');
	strMRUPath[idx]=0;

	RegCloseKey(hKEY);
	return TRUE;
}

  

原文地址:https://www.cnblogs.com/flysnail/p/2174905.html