在StreamReader上使用Environment.UserName

时间:2017-03-29 14:11:00

标签: vb.net 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

2 个答案:

答案 0 :(得分:0)

喜欢这个吗?

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

答案 1 :(得分:0)

Bali C's answer添加我还会实施Using,这有助于处理StreamReader,最好使用Path.Combine

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

    'Do your work

End Using