VB.NET:特定目录中的 GetFiles()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25046290/
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
VB.NET: GetFiles() in Specific Directory
提问by Commmett
Currently the line of code looks like this:
目前这行代码如下所示:
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
The problem is, sometimes, in my case, the path looks something like:
问题是,有时,就我而言,路径看起来像:
C:\Users\c.brummett\Downloads
C:\Users\c.brummett\Downloads
and the split causes a split in the username. How can I avoid this problem but still split by periods? I'm sorry I don't how to make this more relatable.
并且拆分导致用户名拆分。我怎样才能避免这个问题,但仍然按句点分割?对不起,我不知道如何使这更相关。
My idea was to use a DirectoryInfoand do something like:
我的想法是使用 aDirectoryInfo并执行以下操作:
Dim di As DirectoryInfo
di = New DirectoryInfo(path)
Dim files() As String = di.GetFiles(filehead & ".*.*.fsi")
Edit:The problem with this second bit of code, is that it returns the error
编辑:第二位代码的问题在于它返回错误
Value of type '1-dimensional array of System.IO.FileInfo' cannot be converted to '1-> dimensional array of String' because 'System.IO.FileInfo' is not derived from 'String'.`
“System.IO.FileInfo 的一维数组”类型的值无法转换为“1-> 字符串的维数组”,因为“System.IO.FileInfo”不是从“String”派生的。`
回答by Alireza
You can forget about getting an array of file names (you don't need that anyway) and iterate on the array of FileInfo:
您可以忘记获取文件名数组(无论如何您都不需要)并在以下数组上迭代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
回答by rheitzman
Note AllDirectories and change in the line doing the splitting. I didn't look at your array structure stuff.
注意 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

