我将首先创建一个代表您的数据的类:
Public Class DiskPartResult
Public Property Volume As String
Public Property Number As Int32
Public Property Ltr As String
Public Property Label As String
Public Property Fs As String
Public Property Type As String
Public Property SizeUnit As String
Public Property Status As String
Public Property Info As String
End Class
现在您可以使用File.ReadLines
以下 Linq 查询来获取相关数据行:
Dim diskPartFileLines = File.ReadLines("Results.txt")
Dim dataLines = From line In diskPartFileLines
Skip While Not line.TrimStart().StartsWith("----------") Skip (1)
Take While line.TrimStart().StartsWith("Volume")
现在您可以填写List(Of DiskPartResult)
:
Dim alldata = New List(Of DiskPartResult)
For Each line As String In dataLines
Dim columns = line.Trim().Split({vbTab}, StringSplitOptions.RemoveEmptyEntries)
If columns.Length <> 9 Then Continue For
Dim data = New DiskPartResult()
data.Volume = columns(0)
data.Number = Int32.Parse(columns(1))
data.Ltr = columns(2)
data.Label = columns(3)
data.Fs = columns(4)
data.Type = columns(5)
Dim sizeInfo = columns(6)
data.Size = Double.Parse(sizeInfo.Split()(0).Trim())
data.SizeUnit = sizeInfo.Split()(1).Trim()
data.Status = columns(7)
data.Info = columns(8)
alldata.Add(data)
Next
如果你想输出它:
For Each dpr As DiskPartResult In alldata
Console.WriteLine("vol({0})", dpr.Volume)
Console.WriteLine(" .volnum={0}", dpr.Number)
Console.WriteLine(" .letter={0}", dpr.Ltr)
Console.WriteLine(" .label={0}", dpr.Label)
Console.WriteLine(" .type={0}", dpr.Type)
Console.WriteLine(" .size={0}", dpr.Size)
' and so on ... '
Next
显然,分隔符不是制表符,是所有空格。有任何想法吗?
然后你可以在你可以从标题行获得的双空白索引上“拆分”,因为数据行使用相同的索引。
我创建了以下扩展方法来获取文本中给定分隔符字符串的所有索引:
<System.Runtime.CompilerServices.Extension()> _
Public Function AllIndexOf(text As String, str As String, comparisonType As StringComparison) As IList(Of Integer)
Dim allIndeces As IList(Of Integer) = New List(Of Integer)()
Dim index As Integer = text.IndexOf(str, comparisonType)
While index <> -1
allIndeces.Add(index)
index = text.IndexOf(str, index + str.Length, comparisonType)
End While
Return allIndeces
End Function
现在您可以使用此代码查询所需的信息:
Dim diskPartFileLines = File.ReadAllLines("Results.txt")
Dim headerLine = (From line In diskPartFileLines
Skip While Not line.TrimStart().StartsWith("----------")).First().Trim()
Dim colStartIndices As IList(Of Int32) = headerLine.AllIndexOf(" ", StringComparison.OrdinalIgnoreCase)
Dim dataLines = From line In diskPartFileLines
Skip While Not line.TrimStart().StartsWith("----------") Skip 1
Take While line.TrimStart().StartsWith("Volume")
Select line.Trim()
Dim alldata = New List(Of DiskPartResult)
然后枚举查询,初始化DiskPartResults
并将它们添加到列表中:
For Each line In dataLines
Dim data = New DiskPartResult()
Dim lastIndex = 0
For i As Int32 = 0 To colStartIndices.Count - 1
Dim index = colStartIndices(i)
Select Case i
Case 0
data.Volume = line.Substring(lastIndex, index - lastIndex).Trim()
Case 1
data.Number = Int32.Parse(line.Substring(lastIndex, index - lastIndex).Trim())
Case 2
data.Ltr = line.Substring(lastIndex, index - lastIndex).Trim()
Case 3
data.Label = line.Substring(lastIndex, index - lastIndex).Trim()
Case 4
data.Fs = line.Substring(lastIndex, index - lastIndex).Trim()
Case 5
data.Type = line.Substring(lastIndex, index - lastIndex).Trim()
Case 6
Dim sizeInfo = line.Substring(lastIndex, index - lastIndex).Trim()
data.Size = Double.Parse(sizeInfo.Split()(0).Trim())
data.SizeUnit = sizeInfo.Split()(1).Trim()
Case 7
data.Status = line.Substring(lastIndex, index - lastIndex).Trim()
Case 8
data.Info = line.Substring(lastIndex, index - lastIndex).Trim()
End Select
lastIndex = index
Next
Next
请注意,For Each
未测试,但它应该给你的想法。