C# 创建隐藏文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/92376/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 11:34:52  来源:igfitidea点击:

Creating hidden folders

提问by TK.

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

有什么方法可以从 C# 中以编程方式在存储设备上创建(我猜是访问)隐藏文件夹?

采纳答案by Tom Ritter

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

回答by Mark Ingram

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

回答by hangy

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.

这里开始

回答by Phil J Pearson

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

回答by Gul Muhammad

Code to get only Root folders path.

仅获取根文件夹路径的代码。

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

就像如果我们有 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/

它将返回根文件夹路径,即 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++;                    
            }