使用 VB.NET 读取行数

发布于 2024-11-26 05:44:23 字数 291 浏览 8 评论 0原文

如何计算我们上传的文件中的行数。该文件可以是文本文件,也可以是 CSV 或 Excel 文件。

我使用以下代码来获取记录计数,如下所示:

Dim FileCount = From lin1 As String In File.ReadAllLines(hidFilePath.Value.ToString())
Let dd = lin1.Count
Select dd
Dim file_count As Integer =  FileCount.Count

但在某些情况下计数是错误的。

How to calculate the number of rows in a file which we have uploaded. The file might be either a text file or a CSV or Excel file.

I'm using the folowing code to get the record count like this:

Dim FileCount = From lin1 As String In File.ReadAllLines(hidFilePath.Value.ToString())
Let dd = lin1.Count
Select dd
Dim file_count As Integer =  FileCount.Count

but in some cases the count is wrong.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

氛圍 2024-12-03 05:44:23

ReadAllLines 返回一个字符串数组,因此您可以简单地获取数组的长度。

Dim file_count As Integer = _
    File.ReadAllLines(hidFilePath.Value.ToString()).Length

ReadAllLines returns a string array, so you can simply take the length of the array.

Dim file_count As Integer = _
    File.ReadAllLines(hidFilePath.Value.ToString()).Length
凉宸 2024-12-03 05:44:23

编辑:快速阅读问题并用循环解决方案回答

您可以将行数设置为整​​数并让读者读到文件末尾。

Dim sr As New StreamReader("file path here")    
Dim lineCount As Integer = System.Text.RegularExpressions.Regex.Split(sr.ReadToEnd(), Environment.NewLine).Length
sr.Close() 

您可以使用计数变量并循环遍历文件,直到没有任何内容为止

        ''name count and set it to 0
        Dim count As Integer
        count = 0  
        Dim obj As StreamReader
        obj = New StreamReader("C:\...\source.txt")
        ''loop through the file until the end
        Do Until obj.ReadLine Is Nothing
            count = count + 1
        Loop
        ''close file and show count
        obj.Close()
        MessageBox.Show(count)

EDIT: read question to fast and answered with a loop solution

You can set your linecount as an integer and have the reader read to the end of the file.

Dim sr As New StreamReader("file path here")    
Dim lineCount As Integer = System.Text.RegularExpressions.Regex.Split(sr.ReadToEnd(), Environment.NewLine).Length
sr.Close() 

You can use a count variable and loop through the file until theres nothing

        ''name count and set it to 0
        Dim count As Integer
        count = 0  
        Dim obj As StreamReader
        obj = New StreamReader("C:\...\source.txt")
        ''loop through the file until the end
        Do Until obj.ReadLine Is Nothing
            count = count + 1
        Loop
        ''close file and show count
        obj.Close()
        MessageBox.Show(count)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文