2

I used this to hide the file:

File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.Hidden);

But when I wanted to unhide that file, the file just got deleted, but in the code I didn't get any error, so the file is still there.

File.SetAttributes(path, File.GetAttributes(path) | ~FileAttributes.Hidden);
Adarsh Ravi
  • 893
  • 1
  • 16
  • 39
  • 1
    Well. Not working so *well*. – rory.ap Apr 28 '17 at 12:00
  • Possible duplicate of [How to remove a single Attribute (e.g. ReadOnly) from a File?](http://stackoverflow.com/questions/7399611/how-to-remove-a-single-attribute-e-g-readonly-from-a-file) – default locale Apr 28 '17 at 12:16
  • 1
    To disable a single bit you need to use `& ~`, not `| ~`. Look for examples in the linked question. If something doesn't work for you, please, update your question with necessary details. – default locale Apr 28 '17 at 12:18

2 Answers2

2

MSDN:

var attributes = File.GetAttributes(fi);
if ((attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
{
    attributes &= ~FileAttributes.Hidden;
    File.SetAttributes(fi, attributes);
}
Adarsh Ravi
  • 893
  • 1
  • 16
  • 39
-3

You can make a simple check with

bool b = File.Exists(String path)
Dharman
  • 30,962
  • 25
  • 85
  • 135
OneByT
  • 23
  • 1
  • 8