vb.net 检查文本文件是否为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19870664/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 15:45:40  来源:igfitidea点击:

Checking if text file is empty

vb.netvisual-studiovisual-studio-2012

提问by Chad

I have the following code:

我有以下代码:

Private Sub btnCreateAccount_Click(sender As Object, e As EventArgs) Handles btnCreateAccount.Click

        Dim fi As New System.IO.FileInfo(strUsersPath)
        Using r As StreamReader = New StreamReader(strUsersPath)
            Dim line As String
            line = r.ReadLine ' nothing happens after this point
            Do While (Not line Is Nothing)

                If String.IsNullOrWhiteSpace(line) Then
                    MsgBox("File is empty, creating master account")
                    Exit Do
                Else
                    MsgBox("Creating normal account")
                End If
                line = r.ReadLine

            Loop
        End Using

End Sub

I am having some problems. Basicaly I have a streamreader opening up a .txt file where the directory is stored in 'strUsersPath'. I am trying to get the code so that if the file is empty, it does one thing, and if the file is not empty (there is a user) then it does another.

我有一些问题。基本上我有一个流阅读器打开一个 .txt 文件,其中的目录存储在“strUsersPath”中。我正在尝试获取代码,以便如果文件为空,它会做一件事,如果文件不为空(有用户),那么它会做另一件事。

If I have a user in my txt file, the code gives the msgbox("creating normal account"), as expected, however when I do not have a user, it does not give me the other msgbox, and I can't seem to work out why. I suspect it is because IsNullOrWhiteSpace is not the right thing to use for this. Any help would be greatly appreciated

如果我的 txt 文件中有用户,则代码会按预期提供 msgbox("creating normal account"),但是当我没有用户时,它不会给我另一个 msgbox,而且我看不到找出原因。我怀疑这是因为 IsNullOrWhiteSpace 不适合用于此目的。任何帮助将不胜感激

EDITThis is the code I have also tried, same result, clicking the button does nothing if there is already a user.

编辑这是我也尝试过的代码,结果相同,如果已经有用户,则单击按钮不执行任何操作。

Private Sub btnCreateAccount_Click(sender As Object, e As EventArgs) Handles btnCreateAccount.Click

        Dim fi As New System.IO.FileInfo(strUsersPath)
       Using r As StreamReader = New StreamReader(Index.strUsersPath)
            Dim line As String
            line = r.ReadLine ' nothing happens after this point
            Do While (Not line Is Nothing)
                fi.Refresh()
                If Not fi.Length.ToString() = 0 Then
                    MsgBox("File is empty, creating master account") ' does not work
                    Exit Do
                Else
                    MsgBox("Creating normal account") ' works as expected
                End If
                line = r.ReadLine

            Loop
        End Using

End Sub

回答by Chad

You do not need a StreamReader for this. All you need is File.ReadAllText

为此,您不需要 StreamReader。所有你需要的是File.ReadAllText

If File.ReadAllText(strUsersPath).Length = 0 Then
    MsgBox("File is empty, creating master account")
Else
    MsgBox("Creating normal account")
End If

回答by mikro

I recommend using this method

我推荐使用这种方法

If New FileInfo(strUsersPath).Length.Equals(0) Then
    'File is empty.
Else
    'File is not empty.
End If