VB.Net/File Directory/Text File Write — различия между версиями

Материал из VB Эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 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