Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
1.00/5 (1 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;
Posted
Comments
Drazen Pupovac 25-Feb-12 4:12am    
Problem is in separating attributes.

VB
If Directory.Exists("D:\Folder") Then
            Dim divinfo As New DirectoryInfo("D:\Folder")
            divinfo.Attributes = FileAttributes.Hidden
        End If
 
Share this answer
 
It will works like this:

C#
string dirPath = @"C:\Test Folder\Folder2";
if (!Directory.Exists(dirPath))
{
    DirectoryInfo di = Directory.CreateDirectory(dirPath);
    di.Attributes = FileAttributes.Directory | FileAttributes.Hidden | FileAttributes.System;
}
 
Share this answer
 
Comments
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.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900