-
Jul 24th, 2024, 08:09 PM
#1
Thread Starter
New Member
[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
-
Jul 24th, 2024, 08:19 PM
#2
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:
Which would initiate the code in that TestWrite1 routine.
Good luck.
-
Jul 25th, 2024, 05:49 AM
#3
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!
-
Jul 25th, 2024, 07:21 AM
#4
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|