Results 1 to 4 of 4

Thread: [RESOLVED] Attempting to write to and read from a file on a hard drive

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2024
    Posts
    7

    Resolved [RESOLVED] Attempting to write to and read from a file on a hard drive

    I am new to VB2022 and I made 4 attempts to write a string to a file on my hard drive. When I click on "Start" everything builds and runs without errors yet none of the files show up in the target folder - "C:\Users\nicho\documents".
    What am I missing?
    The code follows:

    Imports System.IO
    Public Class form1
    Sub TestWrite1()
    My.Computer.FileSystem.WriteAllText("C:\Users\nicho\Documents\test1.txt", "This is new text to be added.", True)

    End Sub
    Sub Testwrite2()
    Dim TextFile As New System.IO.StreamWriter("C:\Users\nicho\Documents\test2.txt")
    TextFile.WriteLine("This is Line 1")
    TextFile.WriteLine("This is Line 2")
    TextFile.Close()

    End Sub
    Sub Testwrite3()

    Dim fileName As String
    fileName = "C:\Users\nicho\Documents\test3.txt"
    Dim file As System.IO.FileStream
    file = System.IO.File.Create(fileName)
    file.Close()

    FileOpen(1, fileName, OpenMode.Output) ' The file is not visible
    Print(1, "Hello World!")
    FileClose(1)
    Shell("explorer " & fileName)
    '''''''''''''''''''''''
    file.Close()
    '''''''''''''''''''''''
    Shell("C:\Users\nicho\Documents\test3.txt", vbNormalFocus)
    End Sub
    Sub TestWrite4()
    Dim myFilename As String = "C:\Users\nicho\Documents\test4.txt"
    Dim Fich As New BinaryWriter(File.Open(myFilename, FileMode.OpenOrCreate))
    Fich.Write("Hello there!")
    Fich.Write("This is a Binary file.")
    Fich.Close()
    End Sub
    End Class

  2. #2
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,324

    Re: Attempting to write to and read from a file on a hard drive

    By all appearances, you have no code present that actually makes a call to execute any of those Subroutines you have set up.

    Your program is like a cookbook sitting on the shelf. The food doesn't make itself.

    You could place a button on the form, and in the Button's Click event you could just have a single line of code like:

    Code:
    TestWrite1
    Which would initiate the code in that TestWrite1 routine.

    Good luck.

  3. #3
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,894

    Re: Attempting to write to and read from a file on a hard drive

    OptionBase1 is correct. Just for grins I tested all four and they work.

    Code:
    Private Sub form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
          TestWrite1()
          Testwrite2()
          Testwrite3()
          TestWrite4()
    End Sub
    Please remember next time...elections matter!

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,670

    Re: Attempting to write to and read from a file on a hard drive

    For the record, you should be using an option that uses a StreamReader/StreamWriter either directly or indirectly, e.g. File.ReadAllText and File.WriteAllLines use those types internally. Don't mess with that VB6 style of I/O and you'd only use a BinaryWriter if you were mixing text with other data types.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width