在 StreamReader 上使用 Environment.UserName

Using Environment.UserName on a StreamReader

在找到要读取的文件时,我需要帮助将 \bob\ 部分更改为类似 \%username%\ 的内容。

我一直在阅读,一切都在说 Environment.UserName 但我如何将其添加到我当前的代码中?

Imports System.IO
Imports System

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim sr As New StreamReader("C:\Users\bob\Dropbox\Apps\Attachments\my.txt")
        Dim word As String = ""
        Dim words(1000) As String
        Dim i As Integer = 0

        Do Until sr.Peek = -1
            word = sr.ReadLine()
            words(i) = word

            mytext.AppendText(vbNewLine + vbNewLine + TimeOfDay + vbNewLine + vbNewLine + words(i) + vbNewLine + "__________________________________________")
            i += 1

        Loop

        sr.Close()

    End Sub

End Class

像这样?

Dim sr As New StreamReader
    ("C:\Users\" + Environment.UserName + "\Dropbox\Apps\Attachments\my.txt")

I would also implement Using which will help with the disposing of the StreamReader and it would be good practice to use Path.Combine 添加:

Using sr As New StreamReader(Path.Combine("C:\Users", Environment.UserName, "Dropbox\Apps\Attachments\my.txt"))

    'Do your work

End Using