TK.
TK.

Reputation: 47913

Creating hidden folders

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

Upvotes: 49

Views: 45758

Answers (5)

Gul Muhammad
Gul Muhammad

Reputation: 1

Code to get only Root folders path.

Like If we have C:/Test/ C:/Test/Abc C:/Test/xyz C:/Test2/ C:/Test2/mnp

It will return root folders path i.e. C:/Test/ C:/Test2/

            int index = 0;
            while (index < lst.Count)
            {
                My obj = lst[index];
                lst.RemoveAll(a => a.Path.StartsWith(obj.Path));
                lst.Insert(index, obj );
                index++;                    
            }

Upvotes: -2

Phil J Pearson
Phil J Pearson

Reputation: 546

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

Upvotes: 7

Mark Ingram
Mark Ingram

Reputation: 73693

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 |= FileAttributes.Hidden;    
}

Upvotes: 31

hangy
hangy

Reputation: 10859

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.

Upvotes: 4

Tom Ritter
Tom Ritter

Reputation: 101400

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; 
}

Upvotes: 134

Related Questions