Tag: getdirectories

c#Directory.GetDirectories不包括文件夹

我试图在“c:\ Users”中迭代窗口中的用户文件夹列表但排除了microsoft内置用户文件夹,下面是我用来完成此专长的代码段但是由于某种原因不按预期工作。 private readonly List _exclusion = new List { “All Users”, “Default”, “LocalService”, “Public”, “Administrator”, “Default User”, “NetworkService” }; public static bool FoundInArray(List arr, string target) { return arr.Exists(p => p.Trim() == target); } foreach (string d in Directory.GetDirectories(sDir).Where(d => !FoundInArray(_exclusion,d))) { richTextBox1.Text += d + Environment.Newline; } 我不确定为什么这不起作用,有人能为我提供一些见解吗?

如何在使用Directory.GetDirectories时排除文件夹

我想返回’SomeFolder’目录中所有子目录的列表, 不包括 ‘Admin’和’Templates’目录。 我有以下文件夹结构(简化): C:\inetpub\wwwroot\MyWebsite\SomeFolder\RandomString C:\inetpub\wwwroot\MyWebsite\SomeFolder\RandomString C:\inetpub\wwwroot\MyWebsite\SomeFolder\RandomString C:\inetpub\wwwroot\MyWebsite\SomeFolder\Admin C:\inetpub\wwwroot\MyWebsite\SomeFolder\Templates ‘SomeFolder’可以包含不同数量的’RandomString’文件夹(大约从10到100)。 这是我尝试过的: var dirs = Directory.GetDirectories(Server.MapPath(“..”)) .Where(s => !s.EndsWith(“Admin”) || !s.EndsWith(“Templates”)); foreach (string dir in dirs) { lit.Text += Environment.NewLine + dir; } 这将返回完整的文件夹列表(如上所示),而不会过滤掉“管理员”和“模板”。 有趣的是,如果我更改LINQ .Where子句以包含 ,而不是排除 ,“管理”和“模板”它可以工作,这意味着它只返回“管理”和“模板”的路径。 .Where(s => s.EndsWith(“Admin”) || s.EndsWith(“Templates”)); 如果LINQ不是解决方案,有没有办法使用GetDirectories SearchPattern过滤掉目录?

检查目录是否可以在C#中访问?

可能重复: .NET – 检查目录是否可访问而无需exception处理 我使用.NET 3.5和C#在Visual Studio 2010中创建一个小文件浏览器,我有这个函数来检查目录是否可访问: RealPath=@”c:\System Volume Information”; public bool IsAccessible() { //get directory info DirectoryInfo realpath = new DirectoryInfo(RealPath); try { //if GetDirectories works then is accessible realpath.GetDirectories(); return true; } catch (Exception) { //if exception is not accesible return false; } } 但我认为对于大目录,尝试让所有子目录检查目录是否可访问可能会很慢。 我正在使用此function来防止在尝试探索受保护文件夹或没有光盘的cd / dvd驱动器时出错(“设备未就绪”错误)。 是否有更好的方法(更快)检查应用程序是否可以访问目录(最好是在NET 3.5中)?