Search This Blog

Tuesday, February 15, 2011

File operations in VB.NET

In this article we will learn how to delete, copy and move files in VB.NET

This method shows how to delete files in a folder.Using System.IO class Directory 

class we will check whether the folder name in "sourcePath" exists or not.If directory

is not present in the disk then we will create it. Then we will check any files are present

in that folder.If present then we loop through all files one by one and delete those file 

using File.Delete method.

Public Sub DeleteFilesFromFolders(ByVal sourcePath As String)
        If (Directory.Exists(DirPath)) Then
            For Each fName As String In Directory.GetFiles(DirPath)
                If File.Exists(fName) Then
                    File.Delete(fName)
                End If
            Next
        End If
    End Sub

 

This method shows how to move files from one folder to another folder.

  Public Sub MoveFiles(ByVal sourcePath As String, ByVal DestinationPath As String)
        If (Directory.Exists(sourcePath)) Then
            For Each fName As String In Directory.GetFiles(sourcePath)
                If File.Exists(fName) Then
                    Dim dFile As String = String.Empty
                    dFile = Path.GetFileName(fName)
                    Dim dFilePath As String = String.Empty
                    dFilePath = DestinationPath + dFile
                    File.Move(fName, dFilePath)
                End If
            Next
        End If
    End Sub
 

This method shows how to copy files from one folder to another folder.

    Public Sub CopyFiles(ByVal sourcePath As String, ByVal DestinationPath As String)
        If (Directory.Exists(sourcePath)) Then
            For Each fName As String In Directory.GetFiles(sourcePath)
                If File.Exists(fName) Then
                    Dim dFile As String = String.Empty
                    dFile = Path.GetFileName(fName)
                    Dim dFilePath As String = String.Empty
                    dFilePath = DestinationPath + dFile
                    File.Copy(fName, dFilePath, True)
                End If
            Next
        End If
    End Sub

Adding data to an existing file using VB.NET

In this article we will learn how to append data to an existing file.

Here I am using FileStream object to write contents to a file.
First a FileSteam and File object is created. Using file object we will compare wether file is exists are not.

If file exits in the disk then we will truncate it otherwise we will create new file and allow that file for read and write using file steam object.

If file already exists then we will open that file in append mode.
Then we will point the stream reader to last line using streamwriter.
Example : sw.BaseStream.Seek(0, SeekOrigin.End)

If file does't exists then we will create new file and open that file in create mode.Then we will point the stream reader to first line using streamwriter.
Example: sw.BaseStream.Seek(0, SeekOrigin.Begin)

The code follows :

Public Sub ExportToFile(ByVal oFileName As String, ByVal myContent As String)

Dim file As FileStream
Dim f As IO.File

If (f.Exists(oFileName)) Then
file = New FileStream(FileToWrite, FileMode.Append, FileAccess.Write)
Else
file = New FileStream(FileToWrite, FileMode.Create, FileAccess.ReadWrite)
End If

Dim sw As New StreamWriter(file, System.Text.Encoding.Default)
If (f.Exists(oFileName)) Then
sw.BaseStream.Seek(0, SeekOrigin.End)
Else
sw.BaseStream.Seek(0, SeekOrigin.Begin)
End If

sw.Write(myContent)

sw.Close()

End Sub