当前位置:   article > 正文

Chromium入坑之旅:base库之Files文件操作学习_base::filepath

base::filepath

一、base::FilePath

base::FilePath path = base::FilePath(L"C:\T.txt");
FilePath AddExtension(StringPieceType extension) ;//增加扩展名
path. AddExtension(L"ZIP"); // C:\T.txt.RAR

二、base::File

base/files/file.h
base::File file = base::File(const FilePath& path, uint32_t flags);
flags取值(可以用 “|”)

enum Flags {
    FLAG_OPEN = 1 << 0,                  // Opens a file, only if it exists.
    FLAG_CREATE = 1 << 1,                // Creates a new file, only if it does not already exist.
    FLAG_OPEN_ALWAYS = 1 << 2,           // May create a new file.
    FLAG_CREATE_ALWAYS = 1 << 3,         // May overwrite an old file.
    FLAG_OPEN_TRUNCATED = 1 << 4,        // Opens a file and truncates it, only if it exists.
    FLAG_READ = 1 << 5,
    FLAG_WRITE = 1 << 6,
    FLAG_APPEND = 1 << 7,
    FLAG_EXCLUSIVE_READ = 1 << 8,        // EXCLUSIVE is opposite of Windows SHARE.
    FLAG_EXCLUSIVE_WRITE = 1 << 9,
    FLAG_ASYNC = 1 << 10,
    FLAG_TEMPORARY = 1 << 11,            // Used on Windows only.
    FLAG_HIDDEN = 1 << 12,               // Used on Windows only.
    FLAG_DELETE_ON_CLOSE = 1 << 13,
    FLAG_WRITE_ATTRIBUTES = 1 << 14,     // Used on Windows only.
    FLAG_SHARE_DELETE = 1 << 15,         // Used on Windows only.
    FLAG_TERMINAL_DEVICE = 1 << 16,      // Serial port flags.
    FLAG_BACKUP_SEMANTICS = 1 << 17,     // Used on Windows only.
    FLAG_EXECUTE = 1 << 18,              // Used on Windows only.
    FLAG_SEQUENTIAL_SCAN = 1 << 19,      // Used on Windows only.
    FLAG_CAN_DELETE_ON_CLOSE = 1 << 20,  // Requests permission to delete a file via DeleteOnClose() (Windows only). See DeleteOnClose() for details.
  };
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

文件打开 :(FLAG_OPEN)
文件打开并读取 :(FLAG_OPEN |FLAG_READ)
文件关闭后自动删除 :(FLAG_OPEN |FLAG_READ|FLAG_DELETE_ON_CLOSE)
文件手动选择关闭后是否删除 :(FLAG_OPEN |FLAG_READ|FLAG_CAN_DELETE_ON_CLOSE)
Close()之前设置DeleteOnClose(bool delete_on_close);
delete_on_close=true;// 关闭后删除
delete_on_close=false;// 关闭后不删除
Close();不需要主动调用,在File类的析构函数已调用改方法。

三、base::PathExists

base/files/file_util.h
bool PathExists(const FilePath& path);//判断某个文件夹是否存在,不存在返回false,存在返回ture

四、base::CreateDirectory

base/files/file_util.h
创建一个文件夹目录

 bool CreateDirectory(const FilePath& full_path)
  • 1
full_path = base::FilePath(L"C:\\123\\456\\"); // c:\123\456\
  CreateDirectory(full_path);//在c盘下创建123文件夹,并在123文件夹下创建456文件夹
  • 1
  • 2

去掉后面的“\”,效果一样

full_path = base::FilePath(L"C:\\123\\456"); // c:\123\456
  CreateDirectory(full_path);//在c盘下创建123文件夹,并在123文件夹下创建456文件夹
  • 1
  • 2

五、base::MakeAbsoluteFilePath

FilePath MakeAbsoluteFilePath(const FilePath& input);// 将相对路径转换为绝对路径
  • 1
base::MakeAbsoluteFilePath(base::FilePath(L"\\angledata\\"))
  • 1

程序在D盘123目录运行 返回 D:\angledata
程序在C盘456目录运行 返回 C:\angledata\

base::ComputeDirectorySize

int64_t ComputeDirectorySize(const FilePath& root_path);// 返回给定路径目录大小
  • 1

base::ComputeDirectorySize(base::FilePath(L"C:\123\"))

base::DeleteFile

bool DeleteFile(const FilePath& path, bool recursive);
  • 1

如果path为目录,则 recursive 必须等于 true,不然无法删除文件夹。
如果path为文件,则 recursive 是否为true,都不会删除同目录下的其他文件和目录。

base::DeleteFileRecursively

bool DeleteFileRecursively(const FilePath& path); 
//等价于
bool DeleteFile(const FilePath& path, true);
  • 1
  • 2
  • 3

base::DeleteFileAfterReboot

bool DeleteFileAfterReboot(const FilePath& path);
//重启后删除文件,文件或目录必须位于"TMP" 文件下,目录必须为空

base::Move

bool Move(const FilePath& from_path, const FilePath& to_path);
from_path 可以是文件或者目录;
to_path 必须是目录;

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/211458
推荐阅读
相关标签
  

闽ICP备14008679号