none
List files actually visible to a user RRS feed

  • Question

  • I'm scratching my head trying to create a list of all files in a directory which are visible to the current user with current "Folder Options." That means it must honor settings for "Show Hidden Files, Folders and drives" and "Hide Protected Operation System Files" and whatever else may affect items visibility in File Explorer. 

    For example, I set Show Hidden files and Hide protected operating system files options and see three files in the working directory. However, DirectoryInfo.GetFiles(...) yields 4 files there. One hidden file with its name starting with ~ is considered "protected operating system file" by Windows explorer and is not shown there. I don't want it to be listed when the above mentioned options are selected.

    So how could I query which folder options are currently selected by user?

    What makes Windows Explorer think a file is "Protected" besides of being hidden and with a filename starting with ~ ?

    Wednesday, November 14, 2018 10:48 PM

Answers

  • For the record, I ended up with special casing hidden files starting with ~
    Will see if anything else unexpected comes. 

    showhidden = ExplorerOptions.ShowHiddenFiles;
    showsuperhidden = ExplorerOptions.ShowSuperHiddenFiles;
    DirectoryInfo d = new DirectoryInfo(path);

    var myfiles = d.GetFiles(pattern).Where(o=>ShowFile(o));

    bool ShowFile(FileInfo fi) 
    {
    if ((fi.Attributes & FileAttributes.Hidden) == (FileAttributes)0)
    return true;
    if (showhidden)
    {
    if (showsuperhidden)
    return true;
    else
    {
    return !((fi.Attributes & FileAttributes.System) == FileAttributes.System || fi.Name.StartsWith("~"));
    }
    }
    return false;
    }

    class ExplorerOptions
    {
    internal static bool ShowHiddenFiles
    {
    get
    {
    var key = @"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced";
    int v = (int)Registry.GetValue(key, "Hidden", 0);
    return v == 1;
    }
    }
    internal static bool ShowSuperHiddenFiles
    {
    get
    {
    var key = @"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced";
    int v = (int)Registry.GetValue(key, "ShowSuperHidden", 0);
    return v == 1;
    }
    }
    }
    • Marked as answer by Aleksoft.net Thursday, November 15, 2018 6:58 PM
    Thursday, November 15, 2018 6:58 PM

All replies

  • What makes Windows Explorer think a file is "Protected" besides of being hidden and with a filename starting with ~ ?

    FILE_ATTRIBUTE_SYSTEM 

    FILE_ATTRIBUTE_HIDDEN

    (File Attribute Constants)


    Thursday, November 15, 2018 5:52 AM
  • Hi Aleksoft.net,

    Thank you for posting here.

    Please try to use high permission. Change asInvoker to requireAdministrator.

    <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, November 15, 2018 6:01 AM
    Moderator
  • I can read attributes just fine. The problem is that hidden files like ~abc.doc do not have the System attribute set, but still act as Protected.
    Thursday, November 15, 2018 4:33 PM
  • On my OS (Windows 10) some files starting with ~ have the attribute FILE_ATTRIBUTE_HIDDEN

    and are not displayed when I hide "protected operating system files"

    Thursday, November 15, 2018 6:12 PM
  • For the record, I ended up with special casing hidden files starting with ~
    Will see if anything else unexpected comes. 

    showhidden = ExplorerOptions.ShowHiddenFiles;
    showsuperhidden = ExplorerOptions.ShowSuperHiddenFiles;
    DirectoryInfo d = new DirectoryInfo(path);

    var myfiles = d.GetFiles(pattern).Where(o=>ShowFile(o));

    bool ShowFile(FileInfo fi) 
    {
    if ((fi.Attributes & FileAttributes.Hidden) == (FileAttributes)0)
    return true;
    if (showhidden)
    {
    if (showsuperhidden)
    return true;
    else
    {
    return !((fi.Attributes & FileAttributes.System) == FileAttributes.System || fi.Name.StartsWith("~"));
    }
    }
    return false;
    }

    class ExplorerOptions
    {
    internal static bool ShowHiddenFiles
    {
    get
    {
    var key = @"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced";
    int v = (int)Registry.GetValue(key, "Hidden", 0);
    return v == 1;
    }
    }
    internal static bool ShowSuperHiddenFiles
    {
    get
    {
    var key = @"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced";
    int v = (int)Registry.GetValue(key, "ShowSuperHidden", 0);
    return v == 1;
    }
    }
    }
    • Marked as answer by Aleksoft.net Thursday, November 15, 2018 6:58 PM
    Thursday, November 15, 2018 6:58 PM