2

How can I create or mark a file as hidden using .NET?

p.campbell
  • 98,673
  • 67
  • 256
  • 322
pvaju896
  • 1,397
  • 6
  • 25
  • 46

4 Answers4

17

Use File.SetAttributes. "Hidden" is just one of many available attributes.

Jacob
  • 77,566
  • 24
  • 149
  • 228
11

You set the hidden attribute of the file.

There are several ways of doing so - with File.SetAttributes or FileInfo.Attributes, you simply set the FileAttributes enumeration flag to hidden:

string path = @"c:\myfile.txt";
File.SetAttributes(path, FileAttributes.Hidden);
Oded
  • 489,969
  • 99
  • 883
  • 1,009
5

I assume you are referring to setting the file attribute to hidden in the file system. Please take a look at this link

airmanx86
  • 992
  • 1
  • 8
  • 16
2

If it's an existing file, i.e. not one you've just created, don't just:

File.SetAttributes(path, FileAttributes.Hidden);

or certain other attributes it may have will be lost, so rather you should:

File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.Hidden);
stovroz
  • 6,835
  • 2
  • 48
  • 59