win32--GetFileAttributes

DWORD d = GetFileAttributes(path.c_str());

根据返回的十进制,对比文件属性,来检索指定文件或目录的文件系统属性。

也可以使用

if ((d & FILE_ATTRIBUTE_ARCHIVE) == FILE_ATTRIBUTE_ARCHIVE)

来快速确定文件是否具有某一属性

另外介绍下path.c_str()

语法:

const char *c_str();

c_str()函数返回一个指向正规C字符串的指针常量, 内容与本string串相同. 

这是为了与c语言兼容,在c语言中没有string类型,故必须通过string类对象的成员函数c_str()把string 对象转换成c中的字符串样式。

#include <Windows.h>
#include <string>
#include <iostream>

using namespace std;

int main()
{
  std::string path;
  path = "C:\\Users\\xxx\\OneDrive\\Documents\\1.docx";
  cout << path << endl;
  DWORD d = GetFileAttributes(path.c_str());

  if ((d & FILE_ATTRIBUTE_ARCHIVE) == FILE_ATTRIBUTE_ARCHIVE)
  {
  // The file isn't available on local storage...

  }


  return 0;
}

 

posted @ 2020-04-10 18:16  strive-sun  阅读(1324)  评论(0编辑  收藏  举报