Rohit
Rohit

Reputation: 3638

How to hide file in C#?

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

Answers (5)

stovroz
stovroz

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

Toye_Brainz
Toye_Brainz

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

Rajesh
Rajesh

Reputation: 6579

File.SetAttributes("pathToFile",FileAttributes.Hidden)

Upvotes: 33

Jeremy Cron
Jeremy Cron

Reputation: 2392

Try something like this:

FileInfo fi = new FileInfo(somefile);                
fi.Attributes = FileAttributes.Hidden;

Upvotes: 4

BFree
BFree

Reputation: 103742

    FileInfo f = new FileInfo(myFileName);
    f.Attributes = FileAttributes.Hidden;

Upvotes: 6

Related Questions