System.IO.File.ReadAllLines() 方法在 vb.net 中带有进度条
System.IO.File.ReadAllLines() method with progress bar in vb.net
我正在使用 System.IO.File.ReadAllLines(TextFileURL) 在 vb.net 中读取一个大文本文件。由于该过程需要几秒钟才能完成,是否可以使用进度条?
.
RawFile = System.IO.File.ReadAllLines(TextFileURL)
lines = RawFile.ToList
If arg = "" Then MsgBox("IMPORTER IS DONE")
.
没有循环或任何可用于更新进度条值的东西。任何想法或解决方法将不胜感激。
您可以使用 ReadLines
而不是 ReadAllLines
正如 docs 所说,当您处理非常大的文件时,ReadLines
会更有效率:
Dim lstOflines as List(Of String)
For Each line As String In File.ReadLines(TextFileURL)
lstOflines.Add(line)
Next line
为了得到总行数,可以根据文件大小来猜测,而不是处理两倍的文件
- 获取文件大小的代码:(在开始处理之前使用)
Dim myFile As New FileInfo(TextFileURL)
Dim sizeInBytes As Long = myFile.Length
下面逐行读取一个相当大的 .TXT 文件并报告进度:
代码:
Imports System.IO
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim dialog As New OpenFileDialog
dialog.Filter = "Text|*.txt"
Dim result = dialog.ShowDialog()
If result <> DialogResult.OK Then
Return
End If
Dim stream = File.OpenRead(dialog.FileName)
Dim reader As New StreamReader(stream)
Dim percentage As Integer
While True
Dim line As String = Await reader.ReadLineAsync()
If line Is Nothing Then
Exit While
End If
' TODO do something with your line
Dim percentD As Double = 1D / stream.Length * stream.Position * 100D
Dim percentI As Integer = Math.Floor(percentD)
If percentI > percentage Then
ProgressBar1.Value = percentI
percentage = percentI
End If
End While
Await stream.DisposeAsync()
End Sub
End Class
结果:
备注:
- 这给流带来了负担,因为最终读取一行是小数据
- 尝试使用缓冲流来降低压力
- 注意我只在整数百分比大于之前的时候报告
- 否则更新进度条时你会不知所措UI
- 有一些琐碎的异步用法,您可能需要整体改进
- 进度条没有完全达到 100%,我让你解决这个问题,很容易做到
我正在使用 System.IO.File.ReadAllLines(TextFileURL) 在 vb.net 中读取一个大文本文件。由于该过程需要几秒钟才能完成,是否可以使用进度条?
.
RawFile = System.IO.File.ReadAllLines(TextFileURL)
lines = RawFile.ToList
If arg = "" Then MsgBox("IMPORTER IS DONE")
.
没有循环或任何可用于更新进度条值的东西。任何想法或解决方法将不胜感激。
您可以使用 ReadLines
而不是 ReadAllLines
正如 docs 所说,当您处理非常大的文件时,ReadLines
会更有效率:
Dim lstOflines as List(Of String)
For Each line As String In File.ReadLines(TextFileURL)
lstOflines.Add(line)
Next line
为了得到总行数,可以根据文件大小来猜测,而不是处理两倍的文件
- 获取文件大小的代码:(在开始处理之前使用)
Dim myFile As New FileInfo(TextFileURL)
Dim sizeInBytes As Long = myFile.Length
下面逐行读取一个相当大的 .TXT 文件并报告进度:
代码:
Imports System.IO
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim dialog As New OpenFileDialog
dialog.Filter = "Text|*.txt"
Dim result = dialog.ShowDialog()
If result <> DialogResult.OK Then
Return
End If
Dim stream = File.OpenRead(dialog.FileName)
Dim reader As New StreamReader(stream)
Dim percentage As Integer
While True
Dim line As String = Await reader.ReadLineAsync()
If line Is Nothing Then
Exit While
End If
' TODO do something with your line
Dim percentD As Double = 1D / stream.Length * stream.Position * 100D
Dim percentI As Integer = Math.Floor(percentD)
If percentI > percentage Then
ProgressBar1.Value = percentI
percentage = percentI
End If
End While
Await stream.DisposeAsync()
End Sub
End Class
结果:
备注:
- 这给流带来了负担,因为最终读取一行是小数据
- 尝试使用缓冲流来降低压力
- 注意我只在整数百分比大于之前的时候报告
- 否则更新进度条时你会不知所措UI
- 有一些琐碎的异步用法,您可能需要整体改进
- 进度条没有完全达到 100%,我让你解决这个问题,很容易做到