Wednesday 25 January 2012

file handling class for vb.net

these is normal file handling class for vb.net.

by use of these class you write single line to file and creat a new file as well as you also delete and file.
you can also write a list of string in single file. the list of string is one dimensional or two or more dimensional also write,.

these class you can use as per your require.

the class is following.
for use these class just create  a object of class and  call function as per your require.


Imports System.IO
Imports System.Text
Imports System

Public Class FilHandling
    'developed by nilesh makavana
    Private _Path As String = "C:\Nile" 'the current working folder
    Private Function FolderNew1(ByVal FolderName As String)
        Try
            If (Not Directory.Exists("" & FolderName & "")) Then
                Directory.CreateDirectory("" & FolderName & " ")
            End If
        Catch ex As Exception
            Return False
            Exit Function
        End Try
        Return True
    End Function
    Public Sub New(ByVal Path As String)
        If Me.FolderNew1(Path) = False Then
            If (Not Directory.Exists("" & _Path & "")) Then
                Directory.CreateDirectory("" & _Path & " ")
            End If
        Else
            _Path = Path
        End If
    End Sub
    Public Function FolderNew(ByVal FolderName As String) As Boolean
        Try
            If (Not Directory.Exists("" & _Path & "\" & FolderName & " ")) Then
                Directory.CreateDirectory("" & _Path & "\" & FolderName & " ")
            End If
        Catch ex As Exception
            Return False
            Exit Function
        End Try
        Return True
    End Function
    Public Function FolderDelete(ByVal FolderName As String) As Boolean
        Try
            If (Directory.Exists("" & _Path & "\" & FolderName & " ")) Then
                Directory.Delete("" & _Path & "\" & FolderName & " ")
            End If
        Catch ex As Exception
            Return False
            Exit Function
        End Try
        Return True
    End Function
    Public Function FolderDelete(ByVal FolderName As String, ByVal DeleteSubFolderAlso As Boolean) As Boolean
        If DeleteSubFolderAlso = True Then
            Try
                If (Directory.Exists("" & _Path & "\" & FolderName & " ")) Then
                    Directory.Delete("" & _Path & "\" & FolderName & " ", True)
                End If
            Catch ex As Exception
                Return False
                Exit Function
            End Try
        Else
            Return False
        End If
        Return True
    End Function
    Public Function NewFile(ByVal FileNAme As String, ByVal extension As String) As Boolean
        'create a new file
        Dim _fileName As FileStream
        Try
            If (Not File.Exists("" & _Path & "\" & FileNAme & "." & extension & "")) Then
                _fileName = File.Create("" & _Path & "\" & FileNAme & "." & extension & "")
                _fileName.Close()
            Else
                Return False
            End If
        Catch ex As Exception
            Return False
            MsgBox("Error in code")
        End Try
        Return True
    End Function
    Public Function NewFile(ByVal FileNAme As String, ByVal extension As String, ByVal DeleteAndCreatenew As Boolean) As Boolean
        'create a new file and if existing than replace it
        Dim _fileName As FileStream
        Try
            If DeleteAndCreatenew = True Then
                _fileName = File.Create("" & _Path & "\" & FileNAme & "." & extension & "")
                _fileName.Close()
            End If
        Catch ex As Exception
            Return False
            MsgBox("Error in code")
        End Try
        Return True
    End Function
    Public Function WriteFileText(ByVal FileNAme As String, ByVal extension As String, ByVal TextContent As String) As Boolean
        'write text content in file
        Try
            Dim _fileName As String = "" & _Path & "\" & FileNAme & "." & extension & ""
            Dim _FilWriter As New System.IO.StreamWriter(_fileName, True)
            _FilWriter.WriteLine(TextContent)
            _FilWriter.Close()
        Catch ex As Exception
            Return False
        End Try
        Return True
    End Function
    Public Function WriteFileList(ByVal FileNAme As String, ByVal extension As String, ByVal TextContent As List(Of String)) As Boolean
        'write list of conten in file Append to file.....
        Me.NewFile(FileNAme, extension)
        Try
            Dim _fileName As String = "" & _Path & "\" & FileNAme & "." & extension & ""
            Dim _FilWriter As New System.IO.StreamWriter(_fileName, True)
            Dim i As Integer = 0
            While i < TextContent.Count
                _FilWriter.WriteLine(TextContent(i))
                i = i + 1
            End While
            _FilWriter.Close()
        Catch ex As Exception
            Return False
            MsgBox("Error in code")
        End Try
        Return True
    End Function
    Public Function WriteFileListOfArray(ByVal FileNAme As String, ByVal extension As String, ByVal TextContent As List(Of List(Of String)), ByVal Append As Boolean) As Boolean
        'write list of array  conten in file Append to file.....
        Dim _fileName As String = ""
        If Append = True Then
            Me.NewFile(FileNAme, extension)
            _fileName = "" & _Path & "\" & FileNAme & "." & extension & ""
        Else
            Dim _fileName1 As FileStream
            _fileName1 = File.Create("" & _Path & "\" & FileNAme & "." & extension & "")
            _fileName1.Close()
            _fileName = "" & _Path & "\" & FileNAme & "." & extension & ""
        End If
        Try
            Dim _FilWriter As New System.IO.StreamWriter(_fileName, True)
            Dim totrow As Integer = Integer.Parse(TextContent.Count.ToString())
            Dim totColunm As Integer = Integer.Parse(TextContent(0).Count.ToString())
            Dim i As Integer = 0
            Dim j As Integer = 0
            For i = 0 To totrow - 1
                For j = 0 To totColunm - 1
                    _FilWriter.Write(TextContent(i)(j))
                    If j < totColunm - 1 Then
                        _FilWriter.Write(vbTab)
                    End If
                Next
                _FilWriter.WriteLine()
            Next
            _FilWriter.Close()
            _FilWriter.AutoFlush = True

        Catch ex As Exception
            Return False
            MsgBox("Error in code")
        End Try
        Return True
    End Function
    Public Function ReadFileInList(ByVal FileName As String, ByVal FileExtension As String) As List(Of String)
        Dim ReadData As New List(Of String)
        Dim File As String = "" & _Path & "\" & FileName & "." & FileExtension & ""
        Dim fileReader As New StreamReader(File)
        Dim sLine As String = ""
        Do
            sLine = fileReader.ReadLine()
            If Not sLine Is Nothing Then
                ReadData.Add(sLine)
            End If
        Loop Until sLine Is Nothing
        fileReader.Close()
        fileReader.DiscardBufferedData()
        fileReader.Dispose()
        Return ReadData
    End Function
    'read file and return the line array
    Public Function ReadFileInListOfArray(ByVal FileName As String, ByVal FileExtension As String) As List(Of List(Of String))
        Dim ReadData As New List(Of String)
        Dim File As String = "" & _Path & "\" & FileName & "." & FileExtension & ""
        Dim fileReader As New StreamReader(File)
        Dim sLine As String = ""
        Do
            sLine = fileReader.ReadLine()
            If Not sLine Is Nothing Then
                ReadData.Add(sLine)
            End If
        Loop Until sLine Is Nothing
        fileReader.Close()
        fileReader.DiscardBufferedData()
        fileReader.Dispose()
        Dim Flag As Integer = 0
        Dim multiList As New List(Of List(Of String))
        multiList.Clear()
        '    MsgBox(ReadData.Count.ToString)
        While Flag < ReadData.Count.ToString
            multiList.Add(New List(Of String))
            Flag += 1
        End While
        Flag = 0
        Dim Flag2 As Integer = 0
        Dim split As String()
        While Flag < ReadData.Count.ToString
            Flag2 = 0
            split = ReadData(Flag).Split(CChar(vbTab))
            While Flag2 < split.Count.ToString
                multiList(Flag).Add(split(Flag2).ToString)
                Flag2 += 1
            End While
            Flag += 1
        End While
        Return multiList
    End Function
    Public Function CountCharacter(ByVal value As String, ByVal ch As Char) As Integer
        Dim cnt As Integer = 0
        For Each c As Char In value
            If c = ch Then cnt += 1
        Next
        Return cnt
    End Function
End Class
'Dim info As Byte() = New UTF8Encoding(True).GetBytes("This is some text in the file.")
'_fileName.Write(info, 0, info.Length)

'Dim fileReader As String
'fileReader = My.Computer.FileSystem.ReadAllText("C:\test.txt")
'MsgBox(fileReader)
'My.Computer.FileSystem.SpecialDirectories.MyDocuments

No comments:

Post a Comment