Show/Hide folders in Windows

Tag: PROGRAM
Edit
Created: 2021/3/8
Updated: 2021/3/8

Linqpad script for showing/hiding folders in a directory.

void Main() {
    
    Directory
    .EnumerateDirectories(@"C:\Users\yourname", "*", SearchOption.TopDirectoryOnly)
    .Select(d => {
        return new { Path = d, toggle = showHideToggleButton(d) }
    ;}).Dump();
    
    
}

Button showHideToggleButton(string path) {
    Action<Button> handler = (btn) => {
        if (isPathHidden(path)) {
            showPath(path);
            btn.Text = "Hide";
        } else {
            hidePath(path);
            btn.Text = "Show";
        }
    };
    
    return new Button(isPathHidden(path) ? "Show" : "Hide", handler);
}

// Define other methods and classes here
bool isPathHidden(string path) {
    return File.GetAttributes(path).HasFlag(FileAttributes.Hidden);
}
void hidePath(string path) {
    File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.Hidden);
}
void showPath(string path) {
    File.SetAttributes(path, File.GetAttributes(path) & ~FileAttributes.Hidden);
}