【问题标题】:Change folder attribute to hidden on iOS (FMX, C++)在 iOS 上将文件夹属性更改为隐藏(FMX、C++)
【发布时间】:2019-08-29 15:46:04
【问题描述】:

我想在运行时创建一个目录并将其隐藏。使用 this example 我尝试了以下代码,它在 Win32 上运行良好,但在 iOS 构建时出错:

UnicodeString TestPath;
TestPath = System::Ioutils::TPath::GetDocumentsPath() + "\\test\\";
TDirectory::CreateDirectory(TestPath);
TFileAttributes dirattribs;
dirattribs = TDirectory::GetAttributes(TestPath);
dirattribs = dirattribs << TFileAttribute::faHidden;
TDirectory::SetAttributes(TestPath, dirattribs);

为 iOS 或 Android 构建时遇到的构建错误是 no member named 'faHidden' in 'System::Ioutils::TFileAttribute'。那么,如何在 iOS 和 Android 上更改文件夹属性?

附言使用 Rad Studio 10.3.2(C++ 生成器)。

【问题讨论】:

    标签: firemonkey c++builder


    【解决方案1】:

    faHidden 未在 Posix 系统上实现。这是 Embarcadero 的 DocWiki 中的 documented behaviorfaHidden 仅适用于 Windows。

    要在 Posix 系统上创建对用户隐藏的文件夹,您可以简单地在文件夹名称前加上一个前导点:

    UnicodeString TestPath = System::Ioutils::TPath::GetDocumentsPath();
    #ifdef _Windows
    TestPath = System::Ioutils::TPath::Combine(TestPath, _D("test"));
    TDirectory::CreateDirectory(TestPath);
    TFileAttributes dirattribs = TDirectory::GetAttributes(TestPath);
    dirattribs = dirattribs << TFileAttribute::faHidden;
    TDirectory::SetAttributes(TestPath, dirattribs);
    #else
    TestPath = System::Ioutils::TPath::Combine(TestPath, _D(".test"));
    TDirectory::CreateDirectory(TestPath);
    #endif
    

    注意,在 Windows 上也可以创建一个带有前导点的文件夹。它不会对文件夹的隐藏属性产生影响,您仍然需要明确设置:

    UnicodeString TestPath = System::Ioutils::TPath::GetDocumentsPath();
    TestPath = System::Ioutils::TPath::Combine(TestPath, _D(".test"));
    TDirectory::CreateDirectory(TestPath);
    #ifdef _Windows
    TFileAttributes dirattribs = TDirectory::GetAttributes(TestPath);
    dirattribs = dirattribs << TFileAttribute::faHidden;
    TDirectory::SetAttributes(TestPath, dirattribs);
    #endif
    

    【讨论】:

    • Remy - 我不得不将#ifdef _Windows 下的第一行改为:TestPath = TestPath + "\\test";
    • @relayman357 不要那样做。请使用TPath::Combine()。我只是忘了保存返回值。我更新了我的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-25
    • 2013-08-05
    • 2013-11-06
    • 1970-01-01
    相关资源
    最近更新 更多