Hi masters,

I try to write and read file into txt file but i dont know how to do it.
Anyone know how to achieve this?

Please help me.
any help will appreciated much.

Best Regards

Dani AI

Generated

A compact, practical follow-up for (building on 's quick example): the classic VB "Open/Print/Close" is fine for tiny scripts, but for safer, clearer code use the .NET System.IO APIs (if you are on VB.NET) or the FileSystemObject in VB6. Below are concise, ready-to-run patterns for writing, appending and reading without repeating the exact code already shown in the thread.

' VB.NET — write (overwrite) safely and with UTF-8
Using sw As New System.IO.StreamWriter("C:\Temp\Test.txt", False, System.Text.Encoding.UTF8)
    sw.WriteLine("Line one from VB.NET")
    sw.WriteLine("Line two")
End Using

' VB.NET — read whole file
Dim text As String = System.IO.File.ReadAllText("C:\Temp\Test.txt", System.Text.Encoding.UTF8)

' VB.NET — append a single line
System.IO.File.AppendAllText("C:\Temp\Test.txt", "Appended line" & Environment.NewLine, System.Text.Encoding.UTF8)
' VB6 — FileSystemObject approach (append and read)
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")

' Append (creates file if missing)
Dim ts As Object
Set ts = fso.OpenTextFile("C:\Temp\Test.txt", 8, True) ' 8 = ForAppending
ts.WriteLine "Added from VB6"
ts.Close

' Read lines
Set ts = fso.OpenTextFile("C:\Temp\Test.txt", 1) ' 1 = ForReading
Do While Not ts.AtEndOfStream
    Debug.Print ts.ReadLine
Loop
ts.Close
Set ts = Nothing
Set fso = Nothing

Key tips and gotchas:

  • Overwrite vs append: in StreamWriter pass True to append or use File.AppendAllText. In VB6 use the FSO append mode constant (8).
  • Always close streams (use Using in VB.NET) to release locks.
  • Check that the directory exists (create it if needed) and that your app has write permission.
  • For Unicode use UTF-8 explicitly; VB6 native text APIs are limited—consider ADODB.Stream for Unicode in VB6.
  • For large files prefer StreamReader and process line-by-line to avoid high memory use.

These patterns cover simple reads/writes and avoid common pitfalls (file locks, encoding, missing directories).

Recommended Answers

All 3 Replies

See this thread

See this thread

Thank you.
How about write it?

Best regards

See If This Helps :

An example of writing a file:
Dim sFileText as String
Dim iFileNo as Integer
iFileNo = FreeFile
'open the file for writing
Open "D:\Test.txt" For Output As #iFileNo
'please note, if this file already exists it will be overwritten!

'write some example text to the file
Print #iFileNo, "first line of text"
Print #iFileNo, " second line of text"
Print #iFileNo, "" 'blank line
Print #iFileNo, "some more text!"

'close the file (if you dont do this, you wont be able to open it again!)
Close #iFileNo
commented: Really a Vig Help +1
commented: Nice :D +3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.