Reputation: 3638
I want to hide a file in c#. I know the file path and can create a FileInfo object.
How can I hide it?
Upvotes: 44
Views: 43084
Reputation: 7065
The previously accepted answer:
File.SetAttributes(path, FileAttributes.Hidden);
will result in certain other attributes it may have being lost, so you should:
File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.Hidden);
Upvotes: 88
Reputation: 100
The FileInfo
hidden attributes might be somewhat useless if the user's
windows is set to display hidden files. It is more advisable to move
file to a new path especially where user don't really navigate to like
C:\Program Files\Common Files
or any path you might feel your user
show lesser interest of visiting before you hide the file.
Upvotes: 4
Reputation: 2392
Try something like this:
FileInfo fi = new FileInfo(somefile);
fi.Attributes = FileAttributes.Hidden;
Upvotes: 4
Reputation: 103742
FileInfo f = new FileInfo(myFileName);
f.Attributes = FileAttributes.Hidden;
Upvotes: 6