VB.NET StreamReader只返回ReadLine / Initialization错误的一个字符?

时间:2014-08-07 01:02:27

标签: vb.net streamreader

我在我的vb.net(.NET Framework 4.5)应用程序中遇到了轻微的不幸事件。在使用此代码启动时,没有任何变化,当我稍后触发事件以更改label11时,我只得到字母e,而我无法解释代码中出现的问题。我认为该文件采用UTF-8编码,但改变编码似乎没有帮助。 steamDisplayName,steamID64和steamID32在程序开头定义为字符串。这是我在启动时执行的代码:

Private Sub mainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim reader As New System.IO.StreamReader("C:\Program Files (x86)\Steam\config\loginusers.vdf")

    steamDisplayName = reader.ReadLine(3)
    steamDisplayName = steamDisplayName.Replace(Chr(34), "")
    steamDisplayName = steamDisplayName.Replace("PersonaName", "")

    steamID64 = reader.ReadLine(5)
    steamID64 = steamID64.Replace(Chr(34), "")

    steamID32 = steamID64.Substring(0, 3) - 61197960265728

    Label11.Text = "Welcome, " + steamDisplayName
    My.Settings.steamID = steamID32
    reader.Close()
End Sub

这是我的C:\ Program Files(x86)\ Steam \ config \ loginusers.vdf:

"users"
{
    "76561198041432370"
    {
    "AccountName"       "snip"
    "PersonaName"       "tsunami"
    "RememberPassword"      "1"
    "Timestamp"     "1407351554"
    "WantsOfflineMode"      "0"
    }
}

1 个答案:

答案 0 :(得分:0)

这不是代码编写服务。但是我正在等待安装VS2013 Update 3并且我感到无聊......下面的代码应该对您有所帮助,它完全未经测试,所以请先仔细检查!

Private Sub mainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim lines As New System.IO.File.ReadAllLines("C:\Program Files (x86)\Steam\config\loginusers.vdf")

    Dim id As Long = 0

    For Each line in lines

        If line.Contains("PersonalName") Then
            Dim parts = line.Split(Chr(34), SplitOptions.RemoveEmptyEntries)
            steamDisplayName = parts(1)

        ElseIf Long.TryParse(line.Trim.Replace(Chr(34), String.Empty), id) Then
            steamID64 = id
            steamID32 = steamID64.Substring(0, 3) - 61197960265728   ' What is this for?!

        Label11.Text = "Welcome, " & steamDisplayName
        My.Settings.steamID = steamID32
    Next
End Sub