FILETIME类型的变量转换为CString

FILETIME类型的变量转换为CString类型的方法
  1.             CString tmp;
  2.             CTime time(wfd.ftLastWriteTime);//先把FILETIME类型的数据转换为CTime类型
  3.             tmp.Format(L"%d年%d月%d日 %d点%d分%d秒",time.GetYear(),time.GetMonth(),time.GetDay(),time.GetHour(),time.GetMinute(),time.GetSecond());
  4.             //再将CTime类型转换为CTime类型
  5.             CString fileWriteTime(tmp);//为了能把字符串连接起来,用CString val()的方法声明
CString中的split方法用在MFC里

  1. CString tmp,tmpstr;
  2. tmpstr = file;
  3. int i = tmpstr.Find(' ');
  4. name = tmpstr.Left(i);
  5. tmp = tmpstr.Mid(i + 1,tmpstr.GetLength());
  6. tmpstr = tmp;
  7. i = tmpstr.Find(' ');
  8. CString date(tmpstr.Left(i));
  9. tmp = tmpstr.Mid(i + 1,tmpstr.GetLength());
  10. tmpstr = tmp;
  11. i = tmpstr.Find(' ');
  12. CString dtime(tmpstr.Left(i));
  13. time = date + dtime;
  14. tmp = tmpstr.Mid(i + 1,tmpstr.GetLength());
  15. path = tmp;
很土的办法
MFC中,通过下列代码修改word文档访问时间失败,请解决该问题 BOOL CWordctrlDlg::SetWordFileTimes(LPCTSTR lpszFilePath, CTime createTime, CTime modifyTime, CTime accessTime) { // 1. 验证文件路径 if (lpszFilePath == NULL || _tcslen(lpszFilePath) == 0) { AfxMessageBox(_T("错误:文件路径为空!")); return FALSE; } // 2. 检查文件存在性 if (_taccess(lpszFilePath, 0) == -1) { AfxMessageBox(_T("错误:文件不存在!\r\n") + CString(lpszFilePath)); return FALSE; } // 3. 处理文件属性(移除只读属性) DWORD dwAttr = GetFileAttributes(lpszFilePath); if (dwAttr == INVALID_FILE_ATTRIBUTES) { AfxMessageBox(_T("获取文件属性失败!")); return FALSE; } if (dwAttr & FILE_ATTRIBUTE_READONLY) { if (!SetFileAttributes(lpszFilePath, dwAttr & ~FILE_ATTRIBUTE_READONLY)) { AfxMessageBox(_T("移除只读属性失败!")); return FALSE; } } // 4. 转换时间格式 FILETIME ftCreate, ftModify, ftAccess; CTimeToFileTime(createTime, ftCreate); CTimeToFileTime(modifyTime, ftModify); CTimeToFileTime(accessTime, ftAccess); // 5. 核心修改:使用最小权限单独设置访问时间 HANDLE hFile = CreateFile( lpszFilePath, FILE_WRITE_ATTRIBUTES, // 关键修改:仅需属性写入权限 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, // 关键修改:移除OVERLAPPED标志 NULL ); if (hFile == INVALID_HANDLE_VALUE) { DWORD dwError = GetLastError(); CString strErr; strErr.Format(_T("打开文件失败!错误代码: %d"), dwError); AfxMessageBox(strErr); return FALSE; } // 6. 优先单独设置访问时间(系统限制下最可能成功的操作) BOOL bSuccess = SetFileTime(hFile, NULL, &ftAccess, NULL); DWORD dwError = bSuccess ? ERROR_SUCCESS : GetLastError(); // 7. 如果访问时间设置成功,再设置其他时间 if (bSuccess) { bSuccess = SetFileTime(hFile, &ftCreate, NULL, &ftModify); if (!bSuccess) dwError = GetLastError(); } CloseHandle(hFile); // 8. 错误处理 if (!bSuccess) { CString strErr; strErr.Format(_T("设置时间失败!错误代码: %d"), dwError); AfxMessageBox(strErr); // 恢复原始属性(如果原本是只读) if (dwAttr & FILE_ATTRIBUTE_READONLY) { SetFileAttributes(lpszFilePath, dwAttr); } } return bSuccess; }
最新发布
09-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值