How can I create or mark a file as hidden using .NET?
Asked
Active
Viewed 1.1k times
4 Answers
17
Use File.SetAttributes. "Hidden" is just one of many available attributes.

Jacob
- 77,566
- 24
- 149
- 228
-
ThanX bro..!! one of other site gave me wrong information that- dotnet can-not hide a file. – pvaju896 Aug 09 '10 at 04:45
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
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