注册文件默认打开程序
void PublicFunctions::RegistFileLinker(CString strExt, CString strAppName, CString strAppKey,
CString strDefaultIcon, CString strDescribe)
{
WCHAR strTemp[_MAX_PATH];
HKEY hKey;
RegCreateKey(HKEY_CLASSES_ROOT, strExt, &hKey);
RegSetValue(hKey, _T(""), REG_SZ, strAppKey, strAppKey.GetLength());
RegCloseKey(hKey);
RegCreateKey(HKEY_CLASSES_ROOT, strAppKey, &hKey);
RegSetValue(hKey, _T(""), REG_SZ, strDescribe, strDescribe.GetLength());
RegCloseKey(hKey);
wsprintf(strTemp, _T("%s\\DefaultIcon"), strAppKey);
RegCreateKey(HKEY_CLASSES_ROOT, strTemp, &hKey);
RegSetValue(hKey, _T(""), REG_SZ, strDefaultIcon, strDefaultIcon.GetLength());
RegCloseKey(hKey);
wsprintf(strTemp, _T("%s\\Shell"), strAppKey);
RegCreateKey(HKEY_CLASSES_ROOT, strTemp, &hKey);
RegSetValue(hKey, _T(""), REG_SZ, _T("Open"), DWORD(wcslen(_T("Open")) + 1));
RegCloseKey(hKey);
wsprintf(strTemp, _T("%s\\Shell\\Open\\Command"), strAppKey);
RegCreateKey(HKEY_CLASSES_ROOT, strTemp, &hKey);
wsprintf(strTemp, _T("%s \"%%1\""), strAppName);
RegSetValue(hKey, _T(""), REG_SZ, strTemp, DWORD(wcslen(strTemp) + 1));
RegCloseKey(hKey);
}
删除文件夹
void PublicFunctions::DeleteDirectory(CString strPath)
{
if (strPath.IsEmpty())
return;
CFileFind Finder;
BOOL bWorking = Finder.FindFile(LPCTSTR(strPath + _T("\\*.*")));
while (bWorking)
{
bWorking = Finder.FindNextFile();
if (!Finder.IsDots())
{
if (Finder.IsDirectory())
{
CString str = Finder.GetFilePath();
DeleteDirectory(str);
}
else
{
DeleteFile(Finder.GetFilePath());
}
}
}
Finder.Close();
RemoveDirectory(strPath);
}
情空文件夹
void PublicFunctions::ClearDirectory(CString strPath)
{
if (strPath.IsEmpty())
return;
CFileFind Finder;
BOOL bWorking = Finder.FindFile(LPCTSTR(strPath + _T("\\*.*")));
while (bWorking)
{
bWorking = Finder.FindNextFile();
if (!Finder.IsDots())
{
if (Finder.IsDirectory())
{
CString str = Finder.GetFilePath();
DeleteDirectory(str);
}
else
{
DeleteFile(Finder.GetFilePath());
}
}
}
Finder.Close();
}
拷贝文件夹
void PublicFunctions::CopyDirectory(CString strSour, CString strDes)
{
CFileFind Finder;
BOOL bWorking = Finder.FindFile(LPCTSTR(strSour + _T("\\*.*")));
while (bWorking)
{
bWorking = Finder.FindNextFile();
if (!Finder.IsDots())
{
if (Finder.IsDirectory())
{
CString str1 = Finder.GetFilePath();
CString str2 = strDes + _T("\\") + Finder.GetFileName();
CreateDirectory(str2, NULL);
CopyDirectory(str1, str2);
}
else
{
CString str1 = Finder.GetFilePath();
CString str2 = strDes + _T("\\") + Finder.GetFileName();
CopyFile(str1, str2, FALSE);
}
}
}
Finder.Close();
}
扫描文件夹下面的一级文件夹
void ScanDirList(CStringArray& dirAry, CString strPath)
{
strPath += _T("\\*.*");
CFileFind fFind;
BOOL bFind = fFind.FindFile(strPath);
while (bFind)
{
bFind = fFind.FindNextFile();
if (fFind.IsDirectory() && !fFind.IsDots())
dirAry.Add(fFind.GetFileTitle());
}
fFind.Close();
}
扫描文件夹下面的图片文件
BOOL IsValidPictureFile(CString strFilePath)
{
Bitmap image(strFilePath);
if( image.GetFlags() == ImageFlagsNone )
return FALSE;
return TRUE;
}
void ScanImageFileList(CStringArray& fileAry, CString strPath)
{
strPath += _T("\\*.*");
CFileFind fFind;
BOOL bFind = fFind.FindFile(strPath);
while (bFind)
{
bFind = fFind.FindNextFile();
if (IsValidPictureFile(fFind.GetFilePath()))
fileAry.Add(fFind.GetFileName());
}
fFind.Close();
}
扫描文件夹下面所有文件
void ScanFileList(CStringArray& fileAry, CString strPath)
{
strPath += _T("\\*.*");
CFileFind fFind;
BOOL bFind = fFind.FindFile(strPath);
while (bFind)
{
bFind = fFind.FindNextFile();
if (!fFind.IsDots())
{
if (fFind.IsDirectory())
{
CString str = fFind.GetFilePath();
ScanFileList(fileAry, str);
}
else
{
fileAry.Add(fFind.GetFilePath());
}
}
}
fFind.Close();
}
创建文件快捷方式
BOOL CreateFileLink(CString strFile, CString strLinker)
{
CoInitialize(NULL);
BOOL bRet = FALSE;
IShellLink* psl = NULL;
if (SUCCEEDED(CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
IID_IShellLink, (LPVOID*)&psl)))
{
IPersistFile* ppf;
psl->SetPath(strFile);
if (SUCCEEDED(psl->QueryInterface( IID_IPersistFile, (LPVOID *) &ppf)))
{
if (SUCCEEDED(ppf->Save(strLinker, TRUE)))
bRet = TRUE;
ppf->Release();
}
psl->Release();
}
CoUninitialize();
return bRet;
}
浏览文件夹
void BrowseFolder(HWND hParent, CString& strFolder)
{
strFolder.Empty();
LPMALLOC lpMalloc;
if (::SHGetMalloc(&lpMalloc) != NOERROR)
return;
WCHAR szDisplayName[_MAX_PATH];
WCHAR szBuffer[_MAX_PATH];
BROWSEINFO browseInfo;
browseInfo.hwndOwner = hParent;
browseInfo.pidlRoot = NULL;
browseInfo.pszDisplayName = szDisplayName;
browseInfo.lpszTitle = _T("BrowseFolder");
browseInfo.ulFlags = BIF_RETURNFSANCESTORS|BIF_RETURNONLYFSDIRS;
browseInfo.lpfn = NULL;
browseInfo.lParam = 0;
LPITEMIDLIST lpItemIDList;
if ((lpItemIDList = ::SHBrowseForFolder(&browseInfo)) != NULL)
{
if (::SHGetPathFromIDList(lpItemIDList, szBuffer))
{
strFolder = szBuffer;
}
}
lpMalloc->Free(lpItemIDList);
lpMalloc->Release();
}
判断文件编码
int GetFileEncoding(CString strPath)
{
CFile file;
file.Open(strPath, CFile::modeRead, NULL);
wchar_t wcText;
file.Read(&wcText, sizeof(wchar_t));
if(wcText == 0xFEFF)
{//UNICODE
return 1;
}
else if(wcText == 0xFFFE)
{//UNICODE BIG ENDINA
return 2;
}
else if(wcText == 0xBBEF)
{//UTF-8
return 3;
}
else
{//ANSI
return 0;
}
}