MFC小笔记之文件

注册文件默认打开程序

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;
}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值