Is there any way that I can programmatically create (and I guess access) hidden folders on a storage device from within c#?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
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;
}
回答2:
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;
}
回答3:
CreateHiddenFolder(string name)
{
DirectoryInfo di = new DirectoryInfo(name);
di.Create();
di.Attributes |= FileAttributes.Hidden;
}
回答4:
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.
回答5:
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++;
}