我正在尝试查询文件夹并仅返回带有数字文件夹名称的文件夹。问题是我在DirectoryInfo[]
对象中需要它。
我可以这样做(并且有效):
List<string> subDirList = Directory.GetDirectories(rootPath, "*", SearchOption.TopDirectoryOnly)
.Where(f => Regex.IsMatch(f, @"[\\/]\d+$")).ToList();
但我真的需要这样的东西:
DirectoryInfo[] subDirs = Directory.GetDirectories(rootPath, "*", SearchOption.TopDirectoryOnly)
.Where(f => Regex.IsMatch(f, @"[\\/]\d+$"));
有什么建议吗?
答案 0 :(得分:5)
您可以使用Sub FindHighestValue()
Dim vData As Variant
Dim Storms As Collection
Dim ndx As Long
Dim ndxStorm As Long
Dim MaxValue As Double
Dim Previous As Double
Dim InStorm As Boolean
vData = Sheet1.Range("I8:I21").Value 'Change this to whatever range your wave heights are in
Set Storms = New Collection
Previous = 0
InStorm = False
For ndx = LBound(vData, 1) To UBound(vData, 1)
If vData(ndx, 1) > 0 And Previous = 0 Then
'A storm has started
MaxValue = vData(ndx, 1)
InStorm = True
End If
If InStorm And vData(ndx, 1) > MaxValue Then
MaxValue = vData(ndx, 1)
End If
If (vData(ndx, 1) = 0 And Previous > 0) Or (ndx = UBound(vData, 1)) Then
'A storm has ended
InStorm = False
Storms.Add MaxValue
End If
Previous = vData(ndx, 1)
Next
If Storms.Count > 0 Then
For ndx = 1 To Storms.Count
Debug.Print ndx & ": " & Storms(ndx)
Next
End If
End Sub
将其映射到.Select(..)
,然后使用DirectoryInfo
:
ToArray()
答案 1 :(得分:2)
实例化DirectoryInfo
类型的对象,然后进行查询。方法DirectoryInfo.GetDirectories将返回所需的DirectoryInfo
DirectoryInfo dirInfo = new DirectoryInfo(yourpath);
DirectoryInfo[] subDirs = dirInfo.GetDirectories("*", SearchOption.TopDirectoryOnly)
.Where(f => Regex.IsMatch(f.FullName, @"[\\/]\d+$")).ToArray();
修改强>
在C#7中,您可以避免使用带有简单int.TryParse
的正则表达式:
DirectoryInfo[] subDirs = dirInfo.GetDirectories("*", SearchOption.TopDirectoryOnly)
.Where(f => int.TryParse(f.Name, out _)).ToArray();
在C#7下面,您需要int
值的out
类型的额外变量