Click here to Skip to main content
14,384,332 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hello There,

I want to Hide Folder Pro-grammatically so that folder can't be view event if we Check - "Show Hidden File and Folder" from "Folder Options".
Also I want to add more attribute so that we can't Delete this Folder

I have below Code. I want to set hide and prevent deletion for "Folder2", but not able hide using Folder options --
Please help!!!

DirectoryInfo dirinfo = new DirectoryInfo("C:\Test Folder\Folder2");
            dirinfo.Attributes = FileAttributes.Hidden;
            dirinfo.Attributes = FileAttributes.System;
Drazen Pupovac 25-Feb-12 4:12am
Problem is in separating attributes.
Rate this:
Please Sign up or sign in to vote.

Solution 1

It will works like this:

string dirPath = @"C:\Test Folder\Folder2";
if (!Directory.Exists(dirPath))
    DirectoryInfo di = Directory.CreateDirectory(dirPath);
    di.Attributes = FileAttributes.Directory | FileAttributes.Hidden | FileAttributes.System;
Pravinkarne.31 25-Feb-12 7:34am
Thank You Very much!!!!!
I was looking for this!!! Thank you!!!
Pravinkarne.31 27-Feb-12 11:09am
Hi Drazen Pupovac,
I have one doubt, I can see the folder from DOS - Cmd prompt. I can change attribute fro DOS as well. Is there any way to prevent this folder to view in DOS?
Drazen Pupovac 29-Feb-12 14:56pm
If you do it with command "dir /ah" it will be possible to see all hidden folders. If you instead of that use command "dir" there will not be hidden folders.
I need more information. Maybe there are another way to do it. You can protect them on another way.

From DOS is possible to see all data, even they are hidden or system folder(or files).
Pravinkarne.31 3-Mar-12 2:37am
Thank You for your help...!!!
My Main purpose of hiding the folder is -
"I have developed one C# Application for my Company. Around 50-60 Users.
Problem is some clever users can delete my application Files, DataBase File, etc.
So want to hide this Completely. Even from DOS and also when I tried to hide folder from above code and I Paste Path in Address bar then I can see data in hidden directory"
So I want to prevent this.

Please Help...!!!
Drazen Pupovac 3-Mar-12 5:46am
Sorry, you can't prevent it 100%.

Only way is to add different role for another users. Then, how to do at? Right click on file (or folder) and properties, then security tab. There you will see all users and groups. Now, for each of them in the top list (except your own login account or Administrator) allow (check) no more then read and read & execute. For Administrator the column Allow should be completely checked.

But, there are some way to get Administrator's password, so if someone wants to delete something, he will do at.
I hope this will solve problems.
Yugank Mittal 88 22-Mar-12 2:57am
If i want the folder back how to get it back
Drazen Pupovac 22-Mar-12 10:12am
I don't know what exactly you think but if you want to change folder attribute, you can do it on this way:

string dirPath = @"C:\Test Folder\Folder2";
if (Directory.Exists(dirPath))
DirectoryInfo di = new DirectoryInfo(dirPath);
di.Attributes = FileAttributes.Normal;
rojaldearintok 5-Jun-14 11:01am
what if the "Hide operating system files is uncheck", you can still see the folder that is hidden, is there way out of this?
Rate this:
Please Sign up or sign in to vote.

Solution 2

If Directory.Exists("D:\Folder") Then
            Dim divinfo As New DirectoryInfo("D:\Folder")
            divinfo.Attributes = FileAttributes.Hidden
        End If

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100