7 answers
I've found out how to write to a file!
Below is the code:
__
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim file As System.IO.StreamWriter
Dim mywriteline, T(3) As String
Dim i As Integer
Dim A, B, C As String
' variables to write
A = "1"
B = "2"
C = "3"
'We put the variables into an array
T(0) = A
T(1) = B
T(2) = C
'Opening the file for writing, erasing existing lines.
'To append data, use True instead of False at the end of the line.
file = My.Computer.FileSystem.OpenTextFileWriter(My.Computer.FileSystem.CurrentDirectory & "\tarif.txt", False)
'writing line by line
mywriteline = ""
Do Until i > 3
mywriteline = T(i)
file.WriteLine(mywriteline)
i = i + 1
Loop
file.Close()
End Sub
__
Below is the code:
__
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim file As System.IO.StreamWriter
Dim mywriteline, T(3) As String
Dim i As Integer
Dim A, B, C As String
' variables to write
A = "1"
B = "2"
C = "3"
'We put the variables into an array
T(0) = A
T(1) = B
T(2) = C
'Opening the file for writing, erasing existing lines.
'To append data, use True instead of False at the end of the line.
file = My.Computer.FileSystem.OpenTextFileWriter(My.Computer.FileSystem.CurrentDirectory & "\tarif.txt", False)
'writing line by line
mywriteline = ""
Do Until i > 3
mywriteline = T(i)
file.WriteLine(mywriteline)
i = i + 1
Loop
file.Close()
End Sub
__
Epica84
You are programming in VB.net and not in VB6, if I'm not mistaken...
lepasprodutous
how do we do it in vb 2010
To write to a file, there are several options:
1. open "C:\essai.txt" for random as #1
put 1,,"Hello" --> to write
get 1,,myVariable --> to read
2. open "C:\essai.txt" for output as #1
write #1, "Hello" --> to write
2a. open "C:\essai.txt" for input as #1
input #1, myVariable --> to read
3. the method with "Append" mentioned by Kalamit
Note: after using a file, you must use the instruction
" close" to close the file.
1. open "C:\essai.txt" for random as #1
put 1,,"Hello" --> to write
get 1,,myVariable --> to read
2. open "C:\essai.txt" for output as #1
write #1, "Hello" --> to write
2a. open "C:\essai.txt" for input as #1
input #1, myVariable --> to read
3. the method with "Append" mentioned by Kalamit
Note: after using a file, you must use the instruction
" close" to close the file.
Indeed, it's very difficult to find information on text file management under VB Express.
For my part, I finally managed to open and read a structured file line by line. I am currently working on writing to the file. Below is the code:
__
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim mystreamreader As IO.StreamReader
Dim myreadline(), T(3) As String
Dim i As Integer
Dim A, B, C As String
' The file is named tarif.txt, contains 3 lines for testing, and is located in the same folder as the executable.
'Opening in read mode
mystreamreader = My.Computer.FileSystem.OpenTextFileReader(My.Computer.FileSystem.CurrentDirectory & "\tarif.txt")
'Reading line by line
Do Until mystreamreader.EndOfStream
myreadline = Split(mystreamreader.ReadLine(), "")
T(i) = myreadline(0)
i = i + 1
Loop
'Inputting data into variables
A = T(0)
B = T(1)
C = T(2)
End Sub
__
Below is the file "Tarif.txt" created with Notepad:
123
456
789
For my part, I finally managed to open and read a structured file line by line. I am currently working on writing to the file. Below is the code:
__
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim mystreamreader As IO.StreamReader
Dim myreadline(), T(3) As String
Dim i As Integer
Dim A, B, C As String
' The file is named tarif.txt, contains 3 lines for testing, and is located in the same folder as the executable.
'Opening in read mode
mystreamreader = My.Computer.FileSystem.OpenTextFileReader(My.Computer.FileSystem.CurrentDirectory & "\tarif.txt")
'Reading line by line
Do Until mystreamreader.EndOfStream
myreadline = Split(mystreamreader.ReadLine(), "")
T(i) = myreadline(0)
i = i + 1
Loop
'Inputting data into variables
A = T(0)
B = T(1)
C = T(2)
End Sub
__
Below is the file "Tarif.txt" created with Notepad:
123
456
789
I'm going back instead of asking the same question, but if I find it, I'll reply to you... Unless you found it?
.
\_/
.
\_/
hi
this small code allows you to write and display the content of the text file
Private Sub Command1_Click()
Dim Response1 As Long
Open "c:\TEST\Rapport.txt" For Append As 1
Print #1, "******************************************"
Print #1, "****************************************"
Print #1, "************************************"
Close
'display the text file
Response = Shell("NotePad.exe c:\TEST\Rapport.txt", vbNormalFocus)
End Sub
this small code allows you to write and display the content of the text file
Private Sub Command1_Click()
Dim Response1 As Long
Open "c:\TEST\Rapport.txt" For Append As 1
Print #1, "******************************************"
Print #1, "****************************************"
Print #1, "************************************"
Close
'display the text file
Response = Shell("NotePad.exe c:\TEST\Rapport.txt", vbNormalFocus)
End Sub
And here’s an interesting link for all those who have the problem... It works like a charm for me (also read the comments from others on this code...)
.
\_/
.
\_/
Ooops, exactly, a slip for the forgetfulness!
http://www.vbfrance.com/article.aspx?Val=3884
There, that’s better!
. .
\_/
http://www.vbfrance.com/article.aspx?Val=3884
There, that’s better!
. .
\_/
Hello,
Open "c:\TEST\error.log" For Append As 1
Print #1, "Error in the PDB To txt procedure"
Print #1, PdbConvert1.GetErrorText
Print #1, ocxerror
Print #1, " "
Print #1, "***************************************"
Print #1, " "
Close
This is a sample code. But check the file opening options on VBFrance, there are several.
a+
Kalamit,
I sand down, therefore I wipe.
Open "c:\TEST\error.log" For Append As 1
Print #1, "Error in the PDB To txt procedure"
Print #1, PdbConvert1.GetErrorText
Print #1, ocxerror
Print #1, " "
Print #1, "***************************************"
Print #1, " "
Close
This is a sample code. But check the file opening options on VBFrance, there are several.
a+
Kalamit,
I sand down, therefore I wipe.