4 vb.net visual-studio visual-studio-2012
我有以下代码:
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
Run Code Online (Sandbox Code Playgroud)
我有一些问题。基本上我有一个流阅读器打开一个 .txt 文件,其中的目录存储在“strUsersPath”中。我试图获取代码,以便如果文件为空,它会做一件事,如果文件不为空(有用户),那么它会做另一件事。
如果我的 txt 文件中有一个用户,代码会按预期提供 msgbox("creating normal account"),但是当我没有用户时,它不会给我另一个 msgbox,而且我看不到找出原因。我怀疑这是因为 IsNullOrWhiteSpace 不适合用于此目的。任何帮助将不胜感激
编辑 这是我也尝试过的代码,结果相同,如果已经有用户,则单击按钮不执行任何操作。
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
Run Code Online (Sandbox Code Playgroud)
为此,您不需要 StreamReader。所有你需要的是File.ReadAllText
If File.ReadAllText(strUsersPath).Length = 0 Then
MsgBox("File is empty, creating master account")
Else
MsgBox("Creating normal account")
End If
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
14157 次 |
最近记录: |