Search code examples
firemonkeyc++builder

Change folder attribute to hidden on iOS (FMX, C++)


I want to create a directory at runtime and make it hidden. Using this example i tried the following code and it works fine on Win32 but errors on iOS build:

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);

The build error i get when building for iOS or Android is no member named 'faHidden' in 'System::Ioutils::TFileAttribute'. So, how can i change folder attributes on iOS and Android?

p.s. Using Rad Studio 10.3.2 (C++ Builder).


Solution

  • faHidden is not implement on Posix systems. This is documented behavior in Embarcadero's DocWiki. faHidden is only available on Windows.

    To create a folder that is hidden from the user on Posix systems, you can simply prepend the folder name with a leading dot:

    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
    

    Note, it is possible to create a folder with a leading dot on Windows, too. It just won't have an effect on the folder's hidden attribute, you would still have to set that explicitly:

    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