Search code examples
vb.netstreamreader

Using Environment.UserName on a StreamReader


I need help in changing the \bob\ part to something like \%username%\ when locating the file to read.

I've been reading and everything is saying Environment.UserName but how do I add this to my current code?

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

Solution

  • Like this?

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