Imports System

Imports System.IO

Imports System.Text

 

Public Class Test

 

    Public Sub readMain()

        'Specify the directories you want to manipulate.

        Dim path As String = "c:\MyDir"

        Dim target As String = "c:\TestDir"

 

        Try

            ' Determine whethers the directory exists.

            If Directory.Exists(path) = False Then

                ' Create the directory.

                Directory.CreateDirectory(path)

            End If

 

            If Directory.Exists(target) Then

                ' Delete the target to ensure it is not there.

                Directory.Delete(target, True)

            End If

 

            ' Move the directory.

            Directory.Move(path, target)

 

            'Create a file in the directory.

            File.CreateText(target + "\myfile.txt")

 

            'Count the files in the target.

            Console.WriteLine("The number of files in {0} is {1}", _

              target, Directory.GetFiles(target).Length)

 

        Catch e As Exception

            Console.WriteLine("The process failed: {0}", e.ToString())

        End Try

    End Sub

    Public Sub streamTest()

 

        Dim path As String = "c:\temp\MyTest.txt"

 

        ' Delete the file if it exists.

        If File.Exists(path) Then

            File.Delete(path)

        End If

 

        'Create the file.

        Dim fs As FileStream = File.Create(path)

 

        AddText(fs, "FileStream class has a rich collection of methods to operate on Files!")

        AddText(fs, "Some of which are explained in this demo,")

        AddText(fs, Environment.NewLine & "and I encoureage you to try out as many examples as possible")

        AddText(fs, Environment.NewLine & Environment.NewLine)

        AddText(fs, "The following is a subset of characters:" & Environment.NewLine)

 

 

        fs.Close()

 

        'Open the stream and read it back.

        fs = File.OpenRead(path)

        Dim b(1024) As Byte

        Dim temp As UTF8Encoding = New UTF8Encoding(True)

 

        Do While fs.Read(b, 0, b.Length) > 0

            Console.WriteLine(temp.GetString(b))

        Loop

 

        fs.Close()

    End Sub

 

    Private Shared Sub AddText(ByVal fs As FileStream, ByVal value As String)

        Dim info As Byte() = New UTF8Encoding(True).GetBytes(value)

        fs.Write(info, 0, info.Length)

    End Sub

 

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        Me.readMain()

    End Sub

 

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        streamTest()

    End Sub

 

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

        Me.Close()

    End Sub

End Class