目前代码行如下所示:
Dim files() As String = System.IO.Directory.GetFiles(path, filehead & ".*.*.fsi")
Dim seqfsi() As Integer
ReDim seqfsi(files.GetUpperBound(0))
Dim args() As String
Dim file As String = ""
For Each file In files
args = Split(file, ".")
If args.Length = 4 Then
seqfsi(System.Array.IndexOf(files, file)) = CInt(args(args.GetUpperBound(0) - 1))
End If
问题是,有时,在我的情况下,路径看起来像:
C:\Users\c.brummett\Downloads
并且拆分导致用户名中的拆分。我怎样才能避免这个问题,但仍然按时期划分?对不起,我不知道如何使这更加相关。
我的想法是使用DirectoryInfo
并执行以下操作:
Dim di As DirectoryInfo
di = New DirectoryInfo(path)
Dim files() As String = di.GetFiles(filehead & ".*.*.fsi")
编辑:第二位代码的问题在于它返回错误
System.IO.FileInfo'类型'1维数组'的值无法转换为'1-> String的维数组'因为'System.IO.FileInfo'不是从'String'派生的。
答案 0 :(得分:2)
您可以忘记获取文件名数组(无论如何都不需要)并迭代FileInfo
数组:
Dim files() As FileInfo = New DirectoryInfo(path).GetFiles(filehead & ".*.*.fsi")
Dim seqfsi() As Integer
ReDim seqfsi(files.GetUpperBound(0))
Dim args() As String
For Each file As FileInfo In files
args = Split(file.Name, ".")
If args.Length = 4 Then
seqfsi(System.Array.IndexOf(files, file)) = CInt(args(args.GetUpperBound(0) - 1))
End If
答案 1 :(得分:1)
注意AllDirectories并更改执行拆分的行。我没有看你的数组结构。
Dim files() As String = System.IO.Directory.GetFiles("C:\temp", "*.doc", IO.SearchOption.AllDirectories)
Dim seqfsi() As Integer
ReDim seqfsi(files.GetUpperBound(0))
Dim args() As String
Dim file As String = ""
For Each file In files
args = file.Substring(file.LastIndexOf("\") + 1).Split(".")
If args.Length = 4 Then
seqfsi(System.Array.IndexOf(files, file)) = CInt(args(args.GetUpperBound(0) - 1))
End If
Next file