none
write text to a file RRS feed

  • Question

  • Hi Everyone:

    I am using vb.net 2015, and have a problem opening a text file and writing a line of text in it.  I know I have to use a StreamWriter, but I just don't know how to implement it.  Here is what I like to do:

    In Routine1, open the file (as new or append, depending on whether the file exists or not)

    In Routine2 write a line of text

    in Routine3 Write some other line of text

    In Routine4 to close and dispose the file

    If all these actions were being done in the same routine, I know how to do it, but I just don't know how to do this in several routines.  I am thinking about creating a file and streamwriter variable and passing them to the routines.  I am not sure if this is possible or not, and if so, how to do it.

    I appreciate it if someone can show me a small code.  Thanks;

    Bob

    Monday, January 21, 2019 3:01 AM

Answers

  • Hi Karen:

    Thanks for your reply.  Let me see if I understand you correctly.  so, you are saying that each time, open the file, write whatever I want into it, and then close it.  Then the next time I want to write into it again, to repeat the same process?  Did I understand you correctly?

    Thanks;

    Bob

    Greetings,

    Yes you understand me. Also the same pattern works for reading and writing to a database, open a connection, do some work then close the connection.

    So this

    Using writer As New StreamWriter(fileName, File.Exists(fileName))
        writer.WriteLine("Mike")
    End Using

    When working with databases

    Public Function LoadCustomers() As DataTable
        Using cn As New OleDbConnection("a connection string")
            Using cmd As New OleDbCommand With {.Connection = cn}
                cmd.CommandText = "Some query"
                Dim dt As New DataTable
                cn.Open()
                dt.Load(cmd.ExecuteReader)
                Return dt
            End Using
        End Using
    End Function

    Both use a "Using" statement which closes (in your case) the stream.

    Also note you need to pass in at the very least a file name when working with SteamWriter which means this must be done as a private variable and that the file dependent on the second argument will be created or set to append mode which is not a good direction to take.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by booboo_US Friday, January 25, 2019 8:51 PM
    Tuesday, January 22, 2019 3:13 AM
    Moderator

All replies

  • Hello,

    You should open the file each time an operation is performed, not leave the file open. So let's say you need to create the file then append a line or if the file exists append.

    The basic logic is where fileName can be a private variable in a form, class or code module.

    Dim fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Names.txt")
    Using writer As New StreamWriter(fileName, File.Exists(fileName))
        writer.WriteLine("Anne")
    End Using

    So if fileName is a private variable or property we use

    Using writer As New StreamWriter(fileName, File.Exists(fileName))
        writer.WriteLine("Mike")
    End Using

    Running both will have two lines afterwards.

    Lastly, sure you can pass a writer around but that is poor programming.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Monday, January 21, 2019 3:35 AM
    Moderator
  • If you want something simple, then consider ‘File.AppendAllText(name, text & vbCrLf)’. It also creates the file.

    Monday, January 21, 2019 6:26 AM
  • Hi,

    see the following link:

    https://docs.microsoft.com/en-us/dotnet/visual-basic/developing-apps/programming/drives-directories-files/writing-to-files

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, January 21, 2019 6:35 AM
  • Hi Karen:

    Thanks for your reply.  Let me see if I understand you correctly.  so, you are saying that each time, open the file, write whatever I want into it, and then close it.  Then the next time I want to write into it again, to repeat the same process?  Did I understand you correctly?

    Thanks;

    Bob

    Tuesday, January 22, 2019 2:03 AM
  • thanks,
    Tuesday, January 22, 2019 2:03 AM
  • thanks.
    Tuesday, January 22, 2019 2:03 AM
  • Hi Karen:

    Thanks for your reply.  Let me see if I understand you correctly.  so, you are saying that each time, open the file, write whatever I want into it, and then close it.  Then the next time I want to write into it again, to repeat the same process?  Did I understand you correctly?

    Thanks;

    Bob

    Greetings,

    Yes you understand me. Also the same pattern works for reading and writing to a database, open a connection, do some work then close the connection.

    So this

    Using writer As New StreamWriter(fileName, File.Exists(fileName))
        writer.WriteLine("Mike")
    End Using

    When working with databases

    Public Function LoadCustomers() As DataTable
        Using cn As New OleDbConnection("a connection string")
            Using cmd As New OleDbCommand With {.Connection = cn}
                cmd.CommandText = "Some query"
                Dim dt As New DataTable
                cn.Open()
                dt.Load(cmd.ExecuteReader)
                Return dt
            End Using
        End Using
    End Function

    Both use a "Using" statement which closes (in your case) the stream.

    Also note you need to pass in at the very least a file name when working with SteamWriter which means this must be done as a private variable and that the file dependent on the second argument will be created or set to append mode which is not a good direction to take.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by booboo_US Friday, January 25, 2019 8:51 PM
    Tuesday, January 22, 2019 3:13 AM
    Moderator
  • Thank you.  I appreciate it.
    Friday, January 25, 2019 8:51 PM