文件夹加密,文件、文件夹

读取文件属性

1.GetFileAttributes

2.
CFile* pFile = pDoc->GetFile(pDoc->GetPathName(),
CFile::modeRead|CFile::shareDenyWrite,NULL);
ASSERT(pFile);
if(pFile)
{
// 保存上次写文件的时间
FILETIME ftLastWriteTime;
BY_HANDLE_FILE_INFORMATION fileinfo;
if(!GetFileInformationByHandle((HANDLE)pFile->m_hFile,&fileinfo))
return 0;
ftLastWriteTime=fileinfo.ftLastWriteTime;
pFile-> Close();
}
3.
//取得文件修改前的修改时间。
CFile MyFile;
if (!MyFile.Open(sTlbPath,CFile::modeRead))
{
AfxMessageBox("打开文件失败.");
return ;
} CFileStatus FileStatus;
MyFile.GetStatus( FileStatus );
time_t cFileTime = FileStatus.m_ctime.GetTime();//文件最近一次创建的时间
time_t mFileTime = FileStatus.m_mtime.GetTime(); //文件最近一次修改的时间
time_t aFileTime = FileStatus.m_atime.GetTime();//文件最近一次访问的时间
MyFile.Close();

判断文件是否存在

1._access
#include <io.h>
if ( _access( "D:\\a.txt", 0 )) != -1 )
{
//not exist.
}
if( (_access( "D:\\a.txt", 2 )) != -1 )
{
// File a.txt has write permission
}
*******************************************************
int _access( const char *path, int mode );
mode Value Checks File For
00 Existence _disibledevent=> {
// File a.txt exists
}
3.FindFirstFile
WIN32_FIND_DATA wFindData;
HANDLE hFind = FindFirstFile("myfile.txt",&wFindData);
if ( hFind != INVALID_HANDLE_VALUE )
{
// File Exists
FindClose(hFind);
}
4.CFile::GetStatus
CFileStatus fs;
if (CFile::GetStatus("myfile.txt",fs))
{
// File a.txt exists
}
5.GetFileAttributes
if ( GetFileAttributes("myfile.txt") != -1 )
{
// File a.txt exists
}
6.PathFileExists (Shell Lightweight Utility API)
#include "shlwapi.h" ( Import Library: Shlwapi.lib )
if ( PathFileExists(myfile.txt") )
{
// File a.txt exists
}
7.PathIsDirectory,文件夹是否存在

删除文件

DeleteFile

创建文件夹

1. CreateDirectory ,父文件夹一定要存在,否则创建失败
2. MakeSureDirectoryPathExists,父文件夹可以不存在。

文件操作函数,如 复制、移动、删除、重命名等

WINSHELLAPI int WINAPI SHFileOperation(
    LPSHFILEOPSTRUCT lpFileOp 
);     

目录函数

Windows目录
GetWindowsDirectory
System目录
GetSystemDirectory
Temp目录
GetTempPath
当前目录
GetCurrentDirectory
应用程序目录
CString path;
GetModuleFileName(NULL,path.GetBufferSetLength(MAX_PATH+1),MAX_PATH);
path.ReleaseBuffer();
int pos = path.ReverseFind('\\');
path = path.Left(pos);

查找文件(夹)

TCHAR szFile[MAX_PATH];
wsprintf(szFile, _T(".\\*.txt"));
CFileFind tempFind;
BOOL isFinded = tempFind.FindFile(szFile);
while (isFinded)
{
isFinded = tempFind.FindNextFile();
if (!tempFind.IsDots())
{
TCHAR szFoundFileName[MAX_PATH];
wcscpy(szFoundFileName, tempFind.GetFileName().GetBuffer(MAX_PATH));
// 是文件
if (!tempFind.IsDirectory())
{
TCHAR szTempFileName[MAX_PATH];
wsprintf(szTempFileName, _T(".\\%s"), szFoundFileName);
}
}
}
tempFind.Close();

文件(夹) 选择对话框

文件选择对话框
CFileDialog,查MSDN,很详细。
下面是MSDN中的一个例子:
void CMyClass::OnFileOpen()
{
// szFilters is a text string that includes two file name filters:
// "*.my" for "MyType Files" and "*.*' for "All Files."
TCHAR szFilters[]= _T("MyType Files (*.my)|*.my|All Files (*.*)|*.*||");
// Create an Open dialog; the default file name extension is ".my".
CFileDialog fileDlg(TRUE, _T("my"), _T("*.my"),
OFN_FILEMUSTEXIST | OFN_HIDEREADONLY, szFilters);
// Display the file dialog. When user clicks OK, fileDlg.DoModal()
// returns IDOK.
if(fileDlg.DoModal() == IDOK)
{
CString pathName = fileDlg.GetPathName();
// Implement opening and reading file in here.
//Change the window's title to the opened file's title.
CString fileName = fileDlg.GetFileTitle();
SetWindowText(fileName);
}
}
文件夹选择对话框
自己参考网上改写的类,如下:
头文件:
#pragma _disibledevent=> ~CFolderDialog();
int DoModal();
TCHAR* GetStringPath();
CString GetPath();
protected:
int DoModalEx();
protected:
HWND m_hPatent;
TCHAR m_pPath[MAX_PATH + 1];
static WNDPROC m_wndProc;
OPENFILENAME m_ofn;
};
源文件:
#include "stdafx.h"
#include "FolderDialog.h"
#include <Dlgs.h>
#include <shobjidl.h>
WNDPROC CFolderDialog::m_wndProc = NULL;
LRESULT CALLBACK WindowProcNew(HWND hwnd,UINT message, WPARAM wParam, LPARAM lParam)
{
CFolderDialog* pThis = (CFolderDialog*)GetWindowLong(hwnd, GWL_USERDATA);
if(pThis == NULL)
{
return -1;
}
if(message == WM_COMMAND && HIWORD(wParam) == BN_CLICKED)
{
if(LOWORD(wParam) == IDOK)
{
::SendMessage(hwnd, CDM_GETFILEPATH, MAX_PATH, (LPARAM)pThis->m_pPath);
HWND cmb13Wnd = GetDlgItem(hwnd, cmb13);
TCHAR szTitle[MAX_PATH] = {0};
::GetWindowText(cmb13Wnd, szTitle, MAX_PATH);
TCHAR* szFileName = PathFindFileName(pThis->m_pPath);
if(_tcsicmp(szTitle, szFileName) != 0)
{
PathRemoveFileSpec(pThis->m_pPath);
PathAppend(pThis->m_pPath, szTitle);
}
if(PathIsDirectory(pThis->m_pPath))
{
EndDialog(hwnd, IDOK);
}
else
{
TCHAR szMsg[2048] = {0};
_stprintf_s(szMsg, 2048, _T("%s doesn't exist!\r\nPlease choose again!"), pThis->m_pPath);
::MessageBox(hwnd, szMsg, _T("Warning"), MB_OK | MB_ICONWARNING);
}
return 0;
}
}
return CallWindowProc(pThis->m_wndProc, hwnd, message, wParam, lParam);
}
UINT_PTR CALLBACK FolderHookProc(HWND hdlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if(uMsg == WM_NOTIFY)
{
LPNMHDR hdr = (LPNMHDR)lParam;
if(hdr->code == CDN_INITDONE)
{
LPOFNOTIFY lpofn = (LPOFNOTIFY)lParam;
CFolderDialog* pThis = (CFolderDialog*)lpofn->lpOFN->lCustData;
HWND hParent = GetParent(hdlg);
::SendMessage(hParent, CDM_HIDECONTROL, stc2, 0);
::SendMessage(hParent, CDM_HIDECONTROL, cmb1, 0);
::SendMessage(hParent, CDM_HIDECONTROL, edt1, 0);
::SendMessage(hParent, CDM_HIDECONTROL, chx1, 0);
::SendMessage(hParent, CDM_SETCONTROLTEXT, stc3, (LPARAM)_T("文件夹名称: "));
::SendMessage(hParent, CDM_SETCONTROLTEXT, IDOK, (LPARAM)_T("选择文件夹"));
RECT rcWnd = {0};
GetWindowRect(hParent, &rcWnd);
rcWnd.bottom -= 30;
SetWindowPos(hParent, NULL, 0, 0, rcWnd.right - rcWnd.left, rcWnd.bottom - rcWnd.top, SWP_NOMOVE | SWP_NOZORDER);
SetWindowLong(hParent, GWL_USERDATA, (long)lpofn->lpOFN->lCustData);
pThis->m_wndProc = (WNDPROC)SetWindowLongPtr(hParent, GWLP_WNDPROC, (LPARAM)WindowProcNew);
}
else if(hdr->code == CDN_SELCHANGE)
{
LPOFNOTIFY lpofn = (LPOFNOTIFY)lParam;
CFolderDialog* pThis = (CFolderDialog*)lpofn->lpOFN->lCustData;
HWND hParent = GetParent(hdlg);
SendMessage(hParent, CDM_GETFILEPATH, MAX_PATH, (LPARAM)pThis->m_pPath);
TCHAR* pFolderName = PathFindFileName(pThis->m_pPath);
SendMessage(hParent, CDM_SETCONTROLTEXT, edt1, (LPARAM)pFolderName);
}
}
return NULL;
}
CFolderDialog::CFolderDialog(const HWND hParent /*= NULL*/, const TCHAR* pPath /*= NULL*/)
: m_hPatent(hParent)
{
ZeroMemory(m_pPath, MAX_PATH + 1);
if (pPath && m_pPath)
{
wcscpy_s(m_pPath, MAX_PATH, pPath);
}
}
CFolderDialog::~CFolderDialog()
{
}
int CFolderDialog::DoModalEx()
{
// 读取操作系统信息
OSVERSIONINFOEX osVersionInfo;
ZeroMemory(&osVersionInfo, sizeof(osVersionInfo));
osVersionInfo.dwOSVersionInfoSize = sizeof(osVersionInfo);
if (GetVersionEx((LPOSVERSIONINFO)&osVersionInfo) == FALSE)
{
return -1;
}
// 如果是Vista 或win7以上操作系统,用新的对话框样式;否则,用老样式
if (osVersionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT
&& osVersionInfo.dwMajorVersion > 5)
{
CComPtr<IFileOpenDialog> dialog;
HRESULT hr = dialog.CoCreateInstance(__uuidof(FileOpenDialog));
if(FAILED(hr) || dialog == NULL)
{
return -1;
}
CComPtr<IShellItem> shellItem;
hr = ::SHCreateItemFromParsingName(m_pPath, 0, IID_IShellItem, reinterpret_cast<void**>(&shellItem));
dialog->SetFolder(shellItem);
dialog->SetOptions(FOS_PICKFOLDERS);
hr = dialog->Show(m_hPatent);
if(S_OK == hr)
{
CComPtr<IShellItem> itemResult;
hr = dialog->GetResult(&itemResult);
TCHAR* buffer = NULL;
hr = itemResult->GetDisplayName(SIGDN_FILESYSPATH, &buffer);
if(SUCCEEDED(hr) && buffer != NULL)
{
_tcscpy_s(m_pPath, MAX_PATH, buffer);
CoTaskMemFree(buffer);
}
return IDOK;
}
return IDCANCEL;
}
else
{
return -1;
}
}
int CFolderDialog::DoModal()
{
if (m_pPath == NULL)
{
return -1;
}
int nRet = DoModalEx();
if(nRet > 0)
{
return nRet;
}
TCHAR szFile[MAX_PATH] = {0};
OPENFILENAME ofn = {0};
ofn.lStructSize = sizeof(ofn);
ofn.lpstrFilter = _T("Folders _disibledevent=> ofn.hwndOwner = m_hPatent;
ofn.lpfnHook = FolderHookProc;
ofn.lpstrFile = szFile;
ofn.nMaxFile = sizeof(szFile);
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lCustData = (LPARAM)this;
ofn.lpstrInitialDir = m_pPath;
BOOL bRet = GetOpenFileName(&ofn);
if(bRet && m_pPath[0] != _T('\0'))
{
return IDOK;
}
return IDCANCEL;
}
TCHAR* CFolderDialog::GetStringPath()
{
if (m_pPath == NULL)
{
return NULL;
}
// --------------------------------------------------------------------------------------------
// I D : 使用下面语句,总是会有内存泄露,而且不能保证用户会删除
// Reason :
//
// Author : Alsmile Date : 2010-7-12
// --------------------------------------------------------------------------------------------
// TCHAR* temp(NULL);
// temp = new TCHAR[MAX_PATH + 1];
// if (temp)
// {
// _tcscpy_s(temp, MAX_PATH, m_pPath);
// }
// return temp;
// --------------------------------------------------------------------------------------------
return m_pPath;
}
CString CFolderDialog::GetPath()
{
CString temp(m_pPath);
return temp;
}
Tags:  隐藏文件夹 文件夹怎么加密 文件夹加密软件 文件夹图标 文件夹加密

延伸阅读

最新评论

发表评论