views:

3030

answers:

4

Is there any way that I can programmatically create (and I guess access) hidden folders on a storage device from within c#?

+19  A: 
using System.IO; 

string path = @"c:\folders\newfolder"; // or whatever 
if (!Directory.Exists(path)) 
{ 
DirectoryInfo di = Directory.CreateDirectory(path); 
di.Attributes = FileAttributes.Directory | FileAttributes.Hidden; 
}
Tom Ritter
First result on google
Tom Ritter
+9  A: 

Yes you can. Create the directory as normal then just set the attributes on it. E.g.

DirectoryInfo di = new DirectoryInfo(@"C:\SomeDirectory");

//See if directory has hidden flag, if not, make hidden

if( (di.Attributes & FileAttributes.Hidden) != FileAttributes.Hidden)

{

     //Add Hidden flag

     di.Attributes = di.Attributes | FileAttributes.Hidden;

 }
Mark Ingram
+3  A: 
string path = @"c:\folders\newfolder"; // or whatever 
if (!System.IO.Directory.Exists(path)) 
{ 
    DirectoryInfo di = Directory.CreateDirectory(path); 
    di.Attributes = FileAttributes.Directory | FileAttributes.Hidden; 
}

From here.

hangy
+5  A: 

CreateHiddenFolder(string name)
{
DirectoryInfo di = new DirectoryInfo(name);
di.Create();
di.Attributes |= FileAttributes.Hidden;
}

Phil J Pearson