0

I have the following code:

If line = Nothing Then
    MsgBox("Login failed.")
        Using sw As New StreamWriter(File.Open(Index.strLogPath, FileMode.OpenOrCreate)) ' open or create a new file at our path
        sw.BaseStream.Seek(0, SeekOrigin.End) ' append new users to the end
        sw.AutoFlush = True
        sw.WriteLine("line is nothing")
        sw.Flush()
        sw.Close()
        End Using

End If

My line = nothing condition is met, the msgbox pops up letting me know, but the file is not created. If the file is there, nothing is added to it.

I have checked the path validity, and ensured that applications have permission there, everything I can think of isn't working, and to make it more frustrating there are no errors! Any help would be greatly appreciated.

4

1 回答 1

1

你知道这个File类已经有一个实用方法可以做到这一点吗?

File.AppendAllText(Index.strLogPath, "line is nothing")

应该就这么简单。:)

编辑

如果您坚持自己管理文件流,请尝试以下操作:

    Using sw As New StreamWriter(File.Open(Index.strLogPath, FileMode.Append)) ' open or create a new file at our path
        sw.WriteLine("line is nothing")
    End Using

兴趣点:

  • 使用FileMode.Append代替OpenOrCreate
  • 无需冲洗或关闭。当您离开“使用”块时,这会自动完成。
于 2013-11-02T21:47:58.233 回答