File Handler class

A great majority of the tasks I'm asked to do I've done many times before, so having a library of code is very helpful. 

This is our FileHandler class, it handles mundane SYSTEM.IO tasks. Like most of the code on this site it's not ground breaking or beautiful, we've got it here to save time on the next project

 

Related Articles

... and you 'll find more on the NET Development Menu

Imports System.IO
Imports System.Security.AccessControl
Imports System.Security.Permissions
 
Public Class FileHandler
    Shared Sub DeleteDocumentsByAge(ByVal strTarget As StringByVal intDeleteFilesOrderThanDays As Int16)
        Try
            Dim directory As New IO.DirectoryInfo(strTarget)
 
            For Each file As IO.FileInfo In directory.GetFiles("*.xml")
                If (Now - file.CreationTime).Days > intDeleteFilesOrderThanDays Then file.Delete()
            Next
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
 
    Shared Sub CreateDirectory(ByVal strTarget As StringByVal strSubdirectory As String)
        Try
            strTarget = Path.Combine(strTarget, strSubdirectory)
            CreateDirectory(strTarget)
 
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
    Shared Sub CreateDirectory(ByVal strTarget As String)
        Try
            If Not Directory.Exists(strTarget) Then
                Directory.CreateDirectory(strTarget)
            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
 
    Shared Sub DeleteFile(ByVal strDirectory As StringByVal strTargetFileName As String)
        Dim strCompleteFileName As String = Path.Combine(strDirectory, strTargetFileName)
        DeleteFile(strCompleteFileName)
    End Sub
 
    Shared Sub DeleteFile(ByVal strCompleteFileName)
        If File.Exists(strCompleteFileName) Then
            File.Delete(strCompleteFileName)
        End If
    End Sub
 
End Class

RealWorldCode gives developers practical, real‑world solutions with clean, working code — no fluff, no theory, just answers.
Links
Home
Knowledge Areas
Sitemap
Contact
Et cetera
Privacy Policy
Terms and Conditions
Cookie Preferences