C#仅查找包含路径一部分的路径
问题描述:
我想搜索包含特定字符串的路径。C#仅查找包含路径一部分的路径
我不知道我想在哪个路径上搜索。
例子:
搜索整个C:\驱动
字符串:Setup \ State中
应该找到这条路径:
C:\ WINDOWS \ SETUP \国家
答
如何:
//checks if directory exists.
if (Directory.Exists(directpryPath))
{
//do something is path exists
}
答
您可以使用Directory.EnumerateDirectories
var paths = Directory.EnumerateDirectories(".. the path you want to search into..", "*.*", SearchOption.AllDirectories)
.Where(path => path.Contains("Setup\State"));
不要忘了命名空间:
using System.IO;
using System.Linq;
+0
谢谢艾哈迈德易卜拉欣。我认为它会工作,但我现在得到一个错误:访问路径:“C:\ Documents and Settings”被拒绝。我该如何解决并允许它? –
+0
@ N.Hoyer我不知道,但有关于这个问题的问题(即http://*.com/questions/10963888和http://*.com/questions/4986293) –
呃,不。问题是“如何找到名称中包含特定字符串的目录”,而不是“如何判断目录是否存在” – stuartd