Set file to hidden

jfritsch_crsjfritsch_crs USUniversity ✭✭

I'm trying to set a file to hidden in the iOS project of my Xamarin.Forms app, but the attributes don't ever seem to stick, even though no error occurs.

File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.Hidden);
var fi = new FileInfo(path);
fi.Attributes |= FileAttributes.Hidden;

Neither of these seem to work. Checking the attributes of the file after shows only one attribute: FileAttributes.Normal.

The documentation makes it seem like I shouldn't have any problem doing this. Am I missing something?

Tagged:

Best Answers

  • jfritsch_crsjfritsch_crs US ✭✭
    Accepted Answer

    Thanks for the follow up, @JGoldberger.

    I'm not sure if you expected adding a . before the filenames would work in iOS, but it definitely did the trick! I renamed some files to include a preceding . and now their sole attribute is FileAttributes.Hidden.

    var fi = new FileInfo(path);
    var newFileName = Path.Combine(fi.DirectoryName, "." + fi.Name);
    File.Move(path, newFileName);
    fi = new FileInfo(newFileName);
    

    So this confirms that files can be hidden, just like folders. Note that you can manually hide folders in a similar fashion.

    var di = new DirectoryInfo(path);
    var newFolderName = Path.Combine(di.Parent.FullName, "." + di.Name);
    Directory.Move(path, newFolderName);
    di = new DirectoryInfo(newFolderName);
    

    This probably also helps confirm your suspicion that the windows file attributes aren't properly mapped.

    My app doesn't actually need to set files as hidden, I just wanted to test that my app worked if it came across hidden files. So while renaming files to have a preceding . may not be the most intuitive, it still worked for what I needed it for.

    Thank you for your help!

Answers

  • SebastianKruseSebastianKruse USMember ✭✭✭

    Certain file attributes, such as FileAttributes.Hidden and FileAttributes.ReadOnly, can be combined. Other attributes, such as FileAttributes.Normal, must be used alone.

    In your code you only append the Hidden Attribute. But if it has already the Normal Attribute, the Normal must be removed first.

  • jfritsch_crsjfritsch_crs USUniversity ✭✭

    Thanks @SebastianKruse, though I've tried that as well and it still doesn't work. Even just removing the Normal attribute does nothing. The file still shows the only attribute being the Normal attribute.

    File.SetAttributes(path, File.GetAttributes(path) & ~FileAttributes.Normal);
    File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.Hidden);
    
  • JGoldbergerJGoldberger USMember, Forum Administrator, Xamarin Team, University Xamurai

    @jfritsch

    Looking over the API docs for FileSystemInfo [1] might it be that you need to call the FileSystemInfo.Refresh() method? From the API docs:

    To get the latest value, call the FileSystemInfo.Refresh method.`

    [1] https://developer.xamarin.com/api/property/System.IO.FileSystemInfo.Attributes/

  • jfritsch_crsjfritsch_crs USUniversity ✭✭
    edited March 2017

    @JGoldberger, that looked promising but it unfortunately didn't work either.

    var fi = new FileInfo(path);
    fi.Attributes |= FileAttributes.Hidden;
    fi.Refresh();
    

    I know at least folders can be hidden, because there is a .config folder in the Documents directory that is hidden. I was curious if maybe the problem was setting something to hidden so I tried to remove the hidden attribute from the folder. Hidden folders have two attributes: FileAttributes.Directory and FileAttributes.Hidden. Visible folders have only one attribute: FileAttributes.Directory.

    var di = new DirectoryInfo(dirPath);
    di.Attributes &= ~FileAttributes.Hidden;
    di.Refresh();
    

    This didn't work either though.

    And just for completeness, I also tried to set a folder as hidden, but it didn't work.

    var di = new DirectoryInfo(dirPath);
    di.Attributes |= FileAttributes.Hidden;
    di.Refresh();
    
  • jfritsch_crsjfritsch_crs USUniversity ✭✭
    Accepted Answer

    Thanks for the follow up, @JGoldberger.

    I'm not sure if you expected adding a . before the filenames would work in iOS, but it definitely did the trick! I renamed some files to include a preceding . and now their sole attribute is FileAttributes.Hidden.

    var fi = new FileInfo(path);
    var newFileName = Path.Combine(fi.DirectoryName, "." + fi.Name);
    File.Move(path, newFileName);
    fi = new FileInfo(newFileName);
    

    So this confirms that files can be hidden, just like folders. Note that you can manually hide folders in a similar fashion.

    var di = new DirectoryInfo(path);
    var newFolderName = Path.Combine(di.Parent.FullName, "." + di.Name);
    Directory.Move(path, newFolderName);
    di = new DirectoryInfo(newFolderName);
    

    This probably also helps confirm your suspicion that the windows file attributes aren't properly mapped.

    My app doesn't actually need to set files as hidden, I just wanted to test that my app worked if it came across hidden files. So while renaming files to have a preceding . may not be the most intuitive, it still worked for what I needed it for.

    Thank you for your help!

  • JGoldbergerJGoldberger USMember, Forum Administrator, Xamarin Team, University Xamurai

    Awesome! I did expect it to hide the file, I did not expect to set the hidden attribute, so bonus.

Sign In or Register to comment.