Efficient way to write to a text file and append?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • solarwind3
    New Member
    • Dec 2013
    • 1

    Efficient way to write to a text file and append?

    I am familiar with two ways of writing to a text file so the data persists when you access it again. Adding more data (from textbox and label) will be necessary for future transactions. I'd like to learn how to write the code so I don't get an UnauthorizedAcc essException class.

    I am using this way, resulting in error:

    Code:
    Dim objwriter As New System.IO.StreamWriter("C:\Customer Transactions.txt")
    
            objwriter.Write(txtCustName.Text, lblOrderNo, lblFinalCost)
            objwriter.Close()

    And there is this way (with errors again):
    Code:
      Dim myStream As Stream
            Dim saveFileDialg As New SaveFileDialog()
    
            saveFileDialg.Filter = ("C:\Customer Transactions.txt")
            saveFileDialg.FilterIndex = 2
            saveFileDialg.RestoreDirectory = True
    
            If saveFileDialg.ShowDialog() = DialogResult.OK Then
                myStream = saveFileDialg.OpenFile()
                If (myStream IsNot Nothing) Then
                    myStream.WriteLine(txtFullName.Text, lblOrder, lblTotal)
                    myStream.Close()
                    myStream.Close()
                End If
            End If

    I'm sure there is something I am not quite getting. Some guidance would be appreciated.
    Last edited by Frinavale; Dec 17 '13, 03:47 PM. Reason: Please use [CODE] and [/CODE] tags when posting code or formatted data.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    The Windows user that is running your application does not have permissions to create files on C:\. You need administrative privileges to change any files in the C:\ root directory.

    Try writing the file into the application's executing path instead. For information about getting the application's path check out this article
    [url=http://msdn.microsoft. com/en-us/library/aa457089.aspx]
    HOW TO: Determine the Executing Application's Path[/icode].

    You could also consider writing into the ProgramData path (for example you could try using C:\ProgramData\ YourApplication Name).

    -Frinny

    Comment

    Working...