MFC::文件

文件

1.01 文件的属性

/*  获得或设置文件的属性::GetFileAttributes(strPathName); */
void CDemoDlg::OnGetFileAttributes() 
{
	//创建文件夹对话框
	CFolderDialog dlg(NULL, NULL, NULL, BIF_BROWSEINCLUDEFILES);

	if (dlg.DoModal() == IDOK)
	{
		//获得文件路径
		CString strPathName = dlg.GetPathName();

		//获得文件属性
		DWORD dwFileAttributes = ::GetFileAttributes(strPathName);

		CString strFileAttributes = _T("");

		if (dwFileAttributes & FILE_ATTRIBUTE_NORMAL)
		{
			strFileAttributes += _T("无\n");
		}
		if (dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
		{
			strFileAttributes += _T("目录\n");
		}
		if (dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE)
		{
			strFileAttributes += _T("存档\n");
		}
		if (dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)
		{
			strFileAttributes += _T("隐藏\n");
		}
		if (dwFileAttributes & FILE_ATTRIBUTE_READONLY)
		{
			strFileAttributes += _T("只读\n");
		}
		if (dwFileAttributes & FILE_ATTRIBUTE_SYSTEM)
		{
			strFileAttributes += _T("系统\n");
		}
		if (dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY)
		{
			strFileAttributes += _T("临时\n");
		}

		CString strText = _T("");
		strText.Format(_T("文件属性:\n%s"), strFileAttributes);
		AfxMessageBox(strText);	
	}
}
/* 设置文件属性::SetFileAttributes */
void CDemoDlg::OnSetFileAttributes() 
{
	//创建文件夹对话框
	CFolderDialog dlg(NULL, NULL, NULL, BIF_BROWSEINCLUDEFILES);

	if (dlg.DoModal() == IDOK)
	{
		//获得文件路径
		CString strPathName = dlg.GetPathName();
	
		DWORD dwFileAttributes = FILE_ATTRIBUTE_ARCHIVE |
			FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_READONLY;

		//设置文件属性
		::SetFileAttributes(strPathName, dwFileAttributes);

		CString strFileAttributes = _T("存档\n隐藏\n只读\n");
		
 		CString strText = _T("");
 		strText.Format(_T("文件属性:\n%s"), strFileAttributes);
 		AfxMessageBox(strText);	
	}
}

1.02 获得文件的信息

void CDemoDlg::OnGetFileInfo()
{
	//创建文件对话框
	CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, _T("所有文件(*.*)|*.*||"));
	if (dlg.DoModal() == IDOK)
	{
		//获得文件路径
		CString strPathName = dlg.GetPathName();

		//获得文件信息
		CFileStatus FileStatus;
		if (CFile::GetStatus(strPathName, FileStatus))
		{
			CString strText = _T("");
			CString strInfo = _T("");

			strInfo.Format(_T("名称:%s\n"), 
				FileStatus.m_szFullName);
			strText += strInfo;

			strInfo.Format(_T("大小:%d字节\n"), 
				FileStatus.m_size);
			strText += strInfo;

			strInfo.Format(_T("创建时间:%d年%d月%d日,%02d:%02d:%02d\n"), 
				FileStatus.m_ctime.GetYear(),
				FileStatus.m_ctime.GetMonth(),
				FileStatus.m_ctime.GetDay(),
				FileStatus.m_ctime.GetHour(),
				FileStatus.m_ctime.GetMinute(),
				FileStatus.m_ctime.GetSecond());
			strText += strInfo;
			strInfo.Format(_T("修改时间:%d年%d月%d日,%02d:%02d:%02d\n"),
				FileStatus.m_ctime.GetYear(),
				FileStatus.m_ctime.GetMonth(),
				FileStatus.m_ctime.GetDay(),
				FileStatus.m_ctime.GetHour(),
				FileStatus.m_ctime.GetMinute(),
				FileStatus.m_ctime.GetSecond());
			strText += strInfo;
			strInfo.Format(_T("访问时间:%d年%d月%d日,%02d:%02d:%02d\n"),
				FileStatus.m_ctime.GetYear(),
				FileStatus.m_ctime.GetMonth(),
				FileStatus.m_ctime.GetDay(),
				FileStatus.m_ctime.GetHour(),
				FileStatus.m_ctime.GetMinute(),
				FileStatus.m_ctime.GetSecond());
 			strText += strInfo;

			CString strFileAttributes = _T("");
			if (FileStatus.m_attribute & 0x00)
			{
				strFileAttributes += _T("无\n");
			}
			if (FileStatus.m_attribute & 0x01)
			{
				strFileAttributes += _T("只读\n");
			}
			if (FileStatus.m_attribute & 0x02)
			{
				strFileAttributes += _T("隐藏\n");
			}
			if (FileStatus.m_attribute & 0x04)
			{
				strFileAttributes += _T("系统\n");
			}
			if (FileStatus.m_attribute & 0x08)
			{
				strFileAttributes += _T("卷标\n");
			}
			if (FileStatus.m_attribute & 0x10)
			{
				strFileAttributes += _T("目录\n");
			}
			if (FileStatus.m_attribute & 0x20)
			{
				strFileAttributes += _T("存档\n");
			}
			strInfo.Format(_T("属性:%s"), strFileAttributes);
			strText += strInfo;

			AfxMessageBox(strText);
		}
	}
}

1.03 使用文件对话框

void CDemoDlg::OnBrowseFile()
{
	//文件扩展名
	CString strFilter = _T("所有文件(*.*)|*.*||");
	//创建文件对话框
	CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | 
		OFN_OVERWRITEPROMPT, strFilter);
	//对话框标题
	dlg.m_ofn.lpstrTitle = _T("浏览文件");

	if(dlg.DoModal() == IDOK)
	{
		//获得文件路径
		CString strPathName = dlg.GetPathName();

		CString strText = _T("");
		strText.Format(_T("%s"), strPathName);
		AfxMessageBox(strText);	
	}
}

1.04 使用文件夹对话框


void CDemoDlg::OnBrowseFolder()
{
	//创建文件夹对话框
	CFolderDialog dlg(NULL, NULL, _T("文件夹列表"), BIF_RETURNONLYFSDIRS);

	if (dlg.DoModal() == IDOK)
	{
		//获得文件夹路径
		CString strPathName = dlg.GetPathName();

		CString strText = _T("");
		strText.Format(_T("%s"), strPathName);
		AfxMessageBox(strText);	
	}
}

1.05 组合框中显示文件和目录列表DlgDirListComboBox

void CDemoDlg::OnTest()
{
	TCHAR szPathName[MAX_PATH] = _T(".");
	UINT nFileType = DDL_READWRITE | DDL_READONLY | DDL_HIDDEN | DDL_SYSTEM | DDL_ARCHIVE;

	//在组合框中显示文件和目录列表
	DlgDirListComboBox(szPathName, IDC_COMBO, 0, nFileType);
	((CComboBox*)GetDlgItem(IDC_COMBO))->SetCurSel(0);

	//在列表框中显示文件和目录列表
	DlgDirList(szPathName, IDC_LIST, 0, nFileType);
}

1.05 复制移动

/* 复制文件,如果目标文件存在将不覆盖 */
::CopyFile(strSourcePathName, strTargetPathName, TRUE);
/* 移动文件 */
::MoveFile(strSourcePathName, strTargetPathName);
/* 删除文件 */
::DeleteFile(strPathName);
/* 重命名文件 */
CFile::Rename(strOldPathName, strNewPathName);

1.06 查找文件

void CDemoDlg::Find(LPCTSTR lpszFileName)
{
	CString strWildcard = lpszFileName;
	strWildcard += _T("\\*.*");

	CFileFind finder;
	BOOL bFind = FALSE;

	// 查找文件
	bFind = finder.FindFile(strWildcard);
	while (bFind)
	{
		// 查找下一个文件
		bFind = finder.FindNextFile();

		// 判断找到文件的是否包含"."或".."
		if (finder.IsDots())
		{
			continue;
		}

		// 获得找到文件的名称
		if (finder.IsDirectory())
		{
			// 找到文件的路径
			CString strFilePath = finder.GetFilePath();
			// 递归查找文件
			Find(strFilePath);
		}
		// 获得找到文件的名称
		CString strFileName = finder.GetFileName();
		CListBox* pListBox = (CListBox*)GetDlgItem(IDC_FILELIST);
		pListBox->AddString(strFileName);
	}
	// 结束查找
	finder.Close();
}

1.07 使用Shell操作文件

CString strSrcFileName = _T("C:\\Demo.dat");
TCHAR szFrom[MAX_PATH];
for (int n = 0; n < MAX_PATH; n++)
{
	szFrom[n] = '\0';
}
StrCat(szFrom, strSrcFileName);

CString strDestFileName = _T("D:\\");
	TCHAR szTo[MAX_PATH];
	for (auto n = 0; n < MAX_PATH; n++)
	{
		szTo[n] = '\0';
	}
StrCat(szTo, strDestFileName);
//复制文件操作
SHFILEOPSTRUCT FileOp;
FileOp.hwnd = NULL;
//复制文件操作
FileOp.wFunc = FO_COPY;
FileOp.pFrom = szFrom;
FileOp.pTo = szTo;
FileOp.fFlags = FOF_SILENT;
//移动文件操作
SHFILEOPSTRUCT FileOp;
FileOp.hwnd = NULL;
FileOp.wFunc = FO_MOVE;
FileOp.pFrom = szFrom;
FileOp.pTo = szTo;
FileOp.fFlags = FOF_SILENT;
//删除文件操作
SHFILEOPSTRUCT FileOp;
FileOp.hwnd = NULL;
FileOp.wFunc = FO_DELETE;
FileOp.pFrom = szFrom;
FileOp.pTo = NULL;
FileOp.fFlags = FOF_SILENT;
//重命名文件操作
SHFILEOPSTRUCT FileOp;
FileOp.hwnd = NULL;
FileOp.wFunc = FO_RENAME;
FileOp.pFrom = szFrom;
FileOp.pTo = szTo;
FileOp.fFlags = FOF_SILENT;
if (SHFileOperation(&FileOp) == 0)

1.08 获得应用程序的目录

void CDemoDlg::OnGetAppDir()
{
	TCHAR szFileName[MAX_PATH];

	//获得应用程序的文件全路径和文件名
	if (::GetModuleFileName(NULL, szFileName, MAX_PATH))
	{
		//去掉文件名
		CString strFileName = szFileName;
		int nIndex = strFileName.ReverseFind('\\');
		CString strDirectory = strFileName.Left(nIndex);

		CString strText = _T("");
		strText.Format(_T("应用程序目录:\n%s"), strDirectory);
		AfxMessageBox(strText);
	}
}

1.09 获得或设置进程的当前目录

void CDemoDlg::OnGetCurDir()
{
	TCHAR szDirectory[MAX_PATH];

	//获得进程的当前目录
	if (::GetCurrentDirectory(MAX_PATH, szDirectory))
	{
		CString strText = _T("");
		strText.Format(_T("进程的当前目录:\n%s"), szDirectory);
		AfxMessageBox(strText);
	}
}
void CDemoDlg::OnSetCurDir() 
{
	CString strDirectory = _T("C:\\");

	//设置进程的当前目录
	if (::SetCurrentDirectory(strDirectory))
	{
		CString strText = _T("");
		strText.Format(_T("进程的当前目录:\n%s"), strDirectory);
		AfxMessageBox(strText);
	}
}

1.10 获得Windows目录和System目录

void CDemoDlg::OnGetWinDir() 
{
	TCHAR szDirectory[MAX_PATH];
	//获得Windows目录
	if (::GetWindowsDirectory(szDirectory, MAX_PATH) > 0)
	{
		CString strText = _T("");
		strText.Format(_T("Windows目录:\n%s"), szDirectory);
		AfxMessageBox(strText);
	}
}
void CDemoDlg::OnGetSysDir()
{
	TCHAR szDirectory[MAX_PATH];
	//获得System目录
	if (::GetSystemDirectory(szDirectory, MAX_PATH) > 0)
	{
		CString strText = _T("");
		strText.Format(_T("System目录:\n%s"), szDirectory);
		AfxMessageBox(strText);
	}
}

1.11 创建临时文件

void CDemoDlg::OnCreateTmpFile() 
{
	TCHAR szPathName[MAX_PATH];
	TCHAR szFileName[MAX_PATH];

	//获得临时文件目录
	if (!::GetTempPath(MAX_PATH, szPathName))
	{
		return;
	}

	//创建临时文件名并在目录中创建文件
	if (!::GetTempFileName(szPathName, _T("~ex"), 0, szFileName))
	{
		return;
	}

	CString strText = _T("");
	strText.Format(_T("临时文件:\n%s"), szFileName);
	AfxMessageBox(strText);
}

1.12 创建目录

void CDemoDlg::OnCreateDir()
{
	CString strDirectory = _T("Demo");
	//查找文件
	CFileFind finder;
	if (finder.FindFile(strDirectory))
	{
		finder.FindNextFile();
		if (finder.IsDirectory())
		{
			AfxMessageBox(_T("目录已经存在。"));
		}
		else
		{
			AfxMessageBox(_T("目录名与现有的文件名重名。"));
		}
	}
	else
	{
		//创建目录
		if (::CreateDirectory(strDirectory, NULL))
		{
			AfxMessageBox(_T("创建目录成功。"));
		}
		else
		{
			AfxMessageBox(_T("创建目录失败。"));
		}
	}
}

1.13 删除目录

BOOL CDemoDlg::DeleteTree(CString strDirectory)
{
	CString strWildcard = strDirectory;
	strWildcard += _T("\\*.*");

	CFileFind finder;
	BOOL bFind = FALSE;

	//查找文件
	bFind = finder.FindFile(strWildcard);
	while (bFind)
	{
		//查找下一个文件
		bFind = finder.FindNextFile();

		if (finder.IsDots())
		{
			continue;
		}

		//找到文件的路径
		CString strPathName = finder.GetFilePath();

		//获得找到文件的名称
		if (finder.IsDirectory())
		{
			//递归删除目录
			if (!DeleteTree(strPathName))
			{
				return FALSE;
			}
		}
		else
		{
			if (!::DeleteFile(strPathName))
			{
				return FALSE;
			}		
		}
	}
	//结束查找
	finder.Close();
	//删除空目录
	if (!::RemoveDirectory(strDirectory))
	{
		return FALSE;
	}
	return TRUE;
}

1.14 逐行读取文本文件CStdioFile

void CDemoDlg::OnReadFile()
{
	CListBox* pListBox = (CListBox*)GetDlgItem(IDC_LIST);
	pListBox->ResetContent();

	//创建文件对话框
	CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY |
		OFN_OVERWRITEPROMPT, _T("文本文件(*.*)|*.*||"));
	if (dlg.DoModal() == IDOK)
	{
		//获得文件路径
		CString strPathName = dlg.GetPathName();
		CStdioFile file;
		//打开文件
		if (!file.Open(strPathName, CFile::modeRead))
		{
			::AfxMessageBox(_T("文件打开失败。"));
			return;
		}
		//读文件
		CString strText = _T("");
		while (file.ReadString(strText))
		{
			pListBox->AddString(strText);
		}
		//关闭文件
		file.Close();
	}
}

1.15 INI文件中读写数据

/* 在INI文件中读字符串数据 */
::GetPrivateProfileString(strSectionName, strKeyName, _T(""), strKeyValue.GetBuffer(1024), 1024, m_strFileName);

/* 在INI文件中写字符串数据 */
::WritePrivateProfileString(strSectionName, strKeyName, strKeyValue, m_strFileName);
void CDemoDlg::OnTest()
{
	CListCtrl* pList = (CListCtrl*)GetDlgItem(IDC_LIST);
	pList->DeleteAllItems();

	TCHAR szKey[1024] = {0};
	CString strKey = _T("");
	CString strKeyName = _T("");
	CString strKeyValue = _T("");

	TCHAR szBuffer[65536] = {0};

	CString strSectionName = _T("");
	GetDlgItemText(IDC_TEXT, strSectionName);

	//获得INI文件指定段的全部键名和键值
	int nBufferSize = GetPrivateProfileSection(strSectionName, szBuffer, 65536, m_strFileName);

	int nItem = 0;
	for (int n = 0, i = 0; n < nBufferSize; n++)
	{
 		if (szBuffer[n] == 0)
 		{
 			szKey[i] = 0;
			strKey = szKey;

			strKeyName = strKey.Left(strKey.Find('='));
			strKeyValue = strKey.Mid(strKey.Find('=') + 1);

			pList->InsertItem(nItem, strKeyName);
			pList->SetItemText(nItem, 1, strKeyValue);
			i = 0;
			nItem++;
 		}
		else
		{
			szKey[i] = szBuffer[n];
			i++;
		}
	}
}
posted @ 2018-09-28 16:01  osbreak  阅读(327)  评论(0编辑  收藏  举报