Search code examples
vb.netauthenticationstreamreaderstreamwriter

Options regarding the properties of Streamreaders and Streamwriters


When using streamreader and streamwriters in code for a login system (where new accounts can be created and existing accounts can be logged into) I have been using:

    usernameReader = New StreamReader("J:\Computing Coursework\real project\KES\Resources\username.txt")

The 'New' part of the streamreader is necessary (as the variable cannot equal just the stream), however I was wondering if there were any other options that I could use instead of the 'as new streamreader' to open the stream without erasing the content of the username text file.

Any help would be appreciated.


Solution

  • You can also use File.OpenText if you do not need to configure the StreamReader for a more specific scenario. The File class provides several useful shortcuts for common I/O tasks.

    By the way: I don't think that opening a StreamReader erases the file; I'd rather suspect that the code that writes to the files is wrong. It will erase the file if you open the file for writing like this:

    writer = new StreamWriter("... path ...")
    

    Have a look at File.AppendText or something similar.