QQ登录

只需一步,快速开始

上位机MFC如何获得或设置文件的属性

[ 复制链接 ]
文件属性的获取可以使用函数GetFileAttributes实现。例如DWORD dwFileAttributes = ::GetFileAttributes(strPathName);
函数返回值有多种数值,每种数值对话一种属性。
具体属性介绍可以参阅MSDN.
常见属性可以参考下面代码。
可以在自己的按钮点击函数内调用。

  1. void CDemoDlg::OnGetFileAttributes()
  2. {
  3.         //创建文件夹对话框
  4.         CFolderDialog dlg(NULL, NULL, NULL, BIF_BROWSEINCLUDEFILES);

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

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

  11.                 CString strFileAttributes = _T("");

  12.                 if (dwFileAttributes & FILE_ATTRIBUTE_NORMAL)
  13.                 {
  14.                         strFileAttributes += _T("无\n");
  15.                 }
  16.                 if (dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  17.                 {
  18.                         strFileAttributes += _T("目录\n");
  19.                 }
  20.                 if (dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE)
  21.                 {
  22.                         strFileAttributes += _T("存档\n");
  23.                 }
  24.                 if (dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)
  25.                 {
  26.                         strFileAttributes += _T("隐藏\n");
  27.                 }
  28.                 if (dwFileAttributes & FILE_ATTRIBUTE_READONLY)
  29.                 {
  30.                         strFileAttributes += _T("只读\n");
  31.                 }
  32.                 if (dwFileAttributes & FILE_ATTRIBUTE_SYSTEM)
  33.                 {
  34.                         strFileAttributes += _T("系统\n");
  35.                 }
  36.                 if (dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY)
  37.                 {
  38.                         strFileAttributes += _T("临时\n");
  39.                 }

  40.                 CString strText = _T("");
  41.                 strText.Format(_T("文件属性:\n%s"), strFileAttributes);
  42.                 AfxMessageBox(strText);       
  43.         }
  44. }
复制代码
另外文件属性的设置对应的实现函数为SetFileAttributes。
例如也可以通过按钮控件调用下面函数来设置文件属性。

  1. void CDemoDlg::OnSetFileAttributes()
  2. {
  3.         //创建文件夹对话框
  4.         CFolderDialog dlg(NULL, NULL, NULL, BIF_BROWSEINCLUDEFILES);

  5.         if (dlg.DoModal() == IDOK)
  6.         {
  7.                 //获得文件路径
  8.                 CString strPathName = dlg.GetPathName();
  9.        
  10.                 DWORD dwFileAttributes = FILE_ATTRIBUTE_ARCHIVE |
  11.                         FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_READONLY;

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

  14.                 CString strFileAttributes = _T("存档\n隐藏\n只读\n");
  15.                
  16.                 CString strText = _T("");
  17.                 strText.Format(_T("文件属性:\n%s"), strFileAttributes);
  18.                 AfxMessageBox(strText);       
  19.         }
  20. }
复制代码


回复

使用道具 举报

点击查看
快速回复 返回列表 客服中心 搜索