VB.Net/File Directory/Text File Write
Версия от 16:40, 26 мая 2010; (обсуждение)
Содержание
Append String to a Text file
Imports System
Imports System.IO
Imports System.Text
Public Class MainClass
Shared Sub Main()
Dim file_stream As New FileStream("test.txt", FileMode.Append)
Dim bytes As Byte() = New UTF8Encoding().GetBytes("Hello world!")
file_stream.Write(bytes, 0, bytes.Length)
file_stream.Close()
End Sub
End Class
Create Text File
Imports System.IO
Module Module1
Sub Main()
Dim TextFile As New StreamWriter("test.txt")
TextFile.WriteLine("Line 1")
TextFile.WriteLine("www.vbex.ru")
TextFile.Close()
Console.WriteLine("Text File created test.txt")
End Sub
End Module
Reading and Writing to Files
Module MainModule
Public Function ReadTextFromFile(ByVal Filename As String) As String
On Error Resume Next
Dim strFileText As String
Dim MyReader As System.IO.StreamReader = System.IO.File.OpenText(Filename)
strFileText = MyReader.ReadToEnd
MyReader.Close()
Return strFileText
End Function
Public Sub WriteTextToFile(ByVal Filename As String, ByVal Text As String)
Dim MyWriter As System.IO.StreamWriter = System.IO.File.CreateText(Filename)
MyWriter.Write(Text)
MyWriter.Close()
End Sub
End Module
Text file Reader and Writer
Imports System
Imports System.IO
Imports System.Text
Public Class MainClass
Shared Sub Main()
Dim stream_writer As New StreamWriter("test.txt")
stream_writer.Write("www.vbex.ru")
stream_writer.WriteLine("www.vbex.ru")
stream_writer.Close()
Dim stream_reader As New StreamReader("test.txt")
Console.WriteLine(stream_reader.ReadToEnd())
stream_reader.Close()
End Sub
End Class