Click here to Skip to main content
15,896,557 members
Articles / Web Development / HTML

Multithreading Backup Utility

Rate me:
Please Sign up or sign in to vote.
4.89/5 (72 votes)
8 Oct 2019CPOL12 min read 239.2K   14.3K   208  
Multithreading is something we will all have to deal with sooner or later. This relatively simple application shows you how to use two threads to copy files. It is also a very handy Windows backup utility with a mirror feature, a batch mode feature, a log file feature and a help button!
Imports System.IO

Public Class Logger

    ' Property variables
    Private _logFile As StreamWriter
    Private _startDateTime As Date
    Private _logFileName As String
    Private _fromPath As String
    Private _toPath As String
    Private _message As String

    ' Constants
    Private Const LOG_FILE As String = "Log.txt"
    Private Const ERR_MSG As String = "Error accessing file: "
    Friend Sub OpenLog()

        If _startDateTime = Nothing Then _startDateTime = Now
        If _logFileName = "" Then _logFileName = Format(_startDateTime, "yyMMdd-hhmmss") & "-" & LOG_FILE

        ' Create log file
        If Not File.Exists(_startDateTime & "-" & LOG_FILE) Then
            Using _logFile As StreamWriter = File.CreateText(_logFileName)
                _logFile.WriteLine("Logfile name is: " & _logFileName)
                _logFile.WriteLine("LOG FILE STARTED AT: " & _startDateTime.ToString)
                _logFile.WriteLine("============================================")
                _logFile.Write(_message)
                _logFile.WriteLine()
                _logFile.Close()
            End Using
        End If

    End Sub
    Friend Sub WriteLog()

        ' Create an instance of StreamWriter to write text to a file.
        Using _logFile As StreamWriter = File.AppendText(_logFileName)
            ' Add some text to the file.
            _logFile.WriteLine()
            _logFile.WriteLine("TIME OF LOG ENTRY: " & DateTime.Now)
            ' Arbitrary objects can also be written to the file.
            _logFile.WriteLine(_message)
            _logFile.Flush()
            _logFile.Close()
        End Using

    End Sub
    Public Property LogFileName() As String

        Get
            Return Format(StartDateTime, "yyMMdd-hhmmss") & "-" & LOG_FILE
        End Get
        Set(ByVal value As String)
            _logFileName = value
        End Set

    End Property
    Public Property StartDateTime() As Date

        Get
            Return _startDateTime
        End Get
        Set(ByVal value As Date)
            _startDateTime = value
        End Set

    End Property
    Public Property Message() As String

        Get
            Return _message
        End Get
        Set(ByVal value As String)
            _message = value
        End Set

    End Property
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Nice people only.
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions