' ***********************************************************************
' Author : Elektro
' Modified : 07-22-2014
' ***********************************************************************
' <copyright file="FileDater.vb" company="Elektro Studios">
' Copyright (c) Elektro Studios. All rights reserved.
' </copyright>
' ***********************************************************************
#Region " Usage Examples "
#Region " Example 1 "
'' Instance the FileDater Class.
'Using fd As New FileDater(File:="C:\Test File.txt")
' ' Preserve the current date-modified of the file.
' fd.Preserve(FileDater.DateType.Modified)
' ' Do some kind of operation that alters the current date-modified of the file.
' IO.File.AppendAllText(fd.File.FullName, New String("X"c, 10I))
' ' Restore the previously preserved date-modified on the TestFile.
' fd.Restore(FileDater.DateType.Modified)
'End Using '/ fd
#End Region
#Region " Example 2 "
'' Instance a test FileInfo using an unique temp file.
'Dim TestFile As New IO.FileInfo(IO.Path.GetTempFileName)
'' Instance the FileDater Class.
'Using fd As New FileDater(File:=TestFile)
' ' Preserve the current dates of the TestFile.
' fd.Preserve(FileDater.DateType.Created Or FileDater.DateType.Accessed Or FileDater.DateType.Modified)
' ' Show the type of the current preserved dates.
' MessageBox.Show(fd.PreservedTypes.ToString)
' ' Show current preserved date-creation.
' MessageBox.Show(fd.PreservedCreationDate.ToString)
' ' Modify the current date-creation on the TestFile.
' fd.Set(FileDater.DateType.Created, Date.Parse("01/01/2015"))
' ' Restore the previously preserved date-creation on the TestFile.
' fd.Restore(FileDater.DateType.Created)
' ' Show the current Creation date of the TestFile.
' MessageBox.Show(TestFile.CreationTime)
'End Using
#End Region
#End Region
#Region " Imports "
Imports System.ComponentModel
Imports System.IO
#End Region
#Region " FileDater "
''' <summary>
''' Contains methods to preserve, set, and restore the dates contained on file.
''' </summary>
Public NotInheritable Class FileDater : Implements IDisposable
#Region " Objects "
''' <summary>
''' Contains the files that are already used in the constructor to prevent a duplicated instance for the same file.
''' </summary>
Private Shared InstancedFiles As New List(Of FileInfo)
#End Region
#Region " Properties "
''' <summary>
''' Gets the file.
''' </summary>
''' <value>The file.</value>
Public ReadOnly Property [File] As FileInfo
Get
Return Me._File
End Get
End Property
Private _File As FileInfo
''' <summary>
''' Gets the type of the current preserved dates.
''' </summary>
Public ReadOnly Property PreservedTypes As DateType
Get
Return Me._PreservedTypes
End Get
End Property
Private _PreservedTypes As DateType = Nothing
''' <summary>
''' Gets the preserved creation date.
''' </summary>
''' <value>The preserved creation date.</value>
Public ReadOnly Property PreservedCreationDate As Date
Get
Return Me._PreservedCreationDate
End Get
End Property
Private _PreservedCreationDate As Date
''' <summary>
''' Gets the preserved last-access date.
''' </summary>
''' <value>The preserved creation date.</value>
Public ReadOnly Property PreservedLastAccessDate As Date
Get
Return Me._PreservedLastAccessDate
End Get
End Property
Private _PreservedLastAccessDate As Date
''' <summary>
''' Gets the preserved last-modify date.
''' </summary>
''' <value>The preserved creation date.</value>
Public ReadOnly Property PreservedLastModifyDate As Date
Get
Return Me._PreservedLastModifyDate
End Get
End Property
Private _PreservedLastModifyDate As Date
#End Region
#Region " Enumerations "
''' <summary>
''' Contains a FileDate flag.
''' </summary>
<FlagsAttribute>
Public Enum DateType As Integer
''' <summary>
''' The date when the file was created.
''' </summary>
Created = 1I
''' <summary>
''' The date when the file was accessed by last time.
''' </summary>
Accessed = 2I
''' <summary>
''' The date when the file was modified by last time.
''' </summary>
Modified = 4I
End Enum
#End Region
#Region " Constructors "
''' <summary>
''' Initializes a new instance of the <see cref="FileDater"/> class.
''' </summary>
''' <param name="File">Indicates the <see cref="FileInfo"/> instance.</param>
''' <exception cref="System.Exception"></exception>
Public Sub New(ByVal [File] As FileInfo
)
If Not InstancedFiles.
Contains([File]) Then InstancedFiles.
Add([File])
Else
Throw New Exception(String.Format("Another instance of the '{0}' class is using the same file.", MyBase.GetType.Name))
End If
End Sub
''' <summary>
''' Initializes a new instance of the <see cref="FileDater"/> class.
''' </summary>
''' <param name="File">Indicates the file.</param>
Public Sub New(ByVal [File] As String) Me.
New(New FileInfo
([File])) End Sub
''' <summary>
''' Prevents a default instance of the <see cref="FileDater"/> class from being created.
''' </summary>
Private Sub New()
End Sub
#End Region
#Region " Hidden Methods "
''' <summary>
''' Serves as a hash function for a particular type.
''' </summary>
<EditorBrowsable(EditorBrowsableState.Never)>
Public Shadows Sub GetHashCode()
End Sub
''' <summary>
''' Determines whether the specified System.Object instances are considered equal.
''' </summary>
<EditorBrowsable(EditorBrowsableState.Never)>
Public Shadows Sub Equals()
End Sub
''' <summary>
''' Determines whether the specified System.Object instances are the same instance.
''' </summary>
<EditorBrowsable(EditorBrowsableState.Never)>
Private Shadows Sub ReferenceEquals()
End Sub
''' <summary>
''' Returns a String that represents the current object.
''' </summary>
<EditorBrowsable(EditorBrowsableState.Never)>
Public Shadows Sub ToString()
End Sub
#End Region
#Region " Public Methods "
''' <summary>
''' Preserves the specified dates of the file to restore them later at any time.
''' Note: Dates can be preserved again at any time.
''' </summary>
''' <param name="DateType">Indicates the type of dates to preserve.</param>
Public Sub Preserve(ByVal DateType As DateType)
Me.DisposedCheck()
' Creation
If DateType.HasFlag(FileDater.DateType.Created) Then
Me._PreservedCreationDate = Me._File.CreationTime
End If
' Accessed
If DateType.HasFlag(FileDater.DateType.Accessed) Then
Me._PreservedLastAccessDate = Me._File.LastAccessTime
End If
' Modified
If DateType.HasFlag(FileDater.DateType.Modified) Then
Me._PreservedLastModifyDate = Me._File.LastWriteTime
End If
Me._PreservedTypes = DateType
End Sub
''' <summary>
''' Preserves at once all the dates of the file to restore them later at any time.
''' Note: Dates can be preserved again at any time.
''' </summary>
Public Sub Preserve()
Me.DisposedCheck()
Me._PreservedCreationDate = Me._File.CreationTime
Me._PreservedLastAccessDate = Me._File.LastAccessTime
Me._PreservedLastModifyDate = Me._File.LastWriteTime
Me._PreservedTypes = DateType.Created Or DateType.Accessed Or DateType.Modified
End Sub
''' <summary>
''' Restores the specified preserved dates on the file.
''' Note: Calling this method does not cause the deletion of any preserved date.
''' </summary>
''' <param name="DateType">Indicates the type of dates to restore on the file.</param>
''' <exception cref="System.Exception">Any date was preserved.</exception>
Public Sub Restore(ByVal DateType As DateType)
Me.DisposedCheck()
' Creation
If DateType.HasFlag(FileDater.DateType.Created) _
AndAlso Me._PreservedTypes.HasFlag(FileDater.DateType.Created) Then
Me._File.CreationTime = Me._PreservedCreationDate
ElseIf DateType.HasFlag(FileDater.DateType.Created) _
AndAlso Not Me._PreservedTypes.HasFlag(FileDater.DateType.Created) Then
Throw New Exception(String.Format("The specified date was not preserved.")) With {
.Source = FileDater.DateType.Created.ToString
}
End If
' Accessed
If DateType.HasFlag(FileDater.DateType.Accessed) _
AndAlso Me._PreservedTypes.HasFlag(FileDater.DateType.Accessed) Then
Me._File.LastAccessTime = Me._PreservedLastAccessDate
ElseIf DateType.HasFlag(FileDater.DateType.Accessed) _
AndAlso Not Me._PreservedTypes.HasFlag(FileDater.DateType.Accessed) Then
Throw New Exception(String.Format("The specified date was not preserved.")) With {
.Source = FileDater.DateType.Accessed.ToString
}
End If
' Modified
If DateType.HasFlag(FileDater.DateType.Modified) _
AndAlso Me._PreservedTypes.HasFlag(FileDater.DateType.Modified) Then
Me._File.LastWriteTime = Me._PreservedLastModifyDate
ElseIf DateType.HasFlag(FileDater.DateType.Modified) _
AndAlso Not Me._PreservedTypes.HasFlag(FileDater.DateType.Modified) Then
Throw New Exception(String.Format("The specified date was not preserved.")) With {
.Source = FileDater.DateType.Modified.ToString
}
End If
End Sub
''' <summary>
''' Restores at once all the preserved dates on the file.
''' Note: Calling this method does not cause the deletion of any preserved date.
''' </summary>
Public Sub Restore()
Me.DisposedCheck()
' Creation
If Me._PreservedTypes.HasFlag(FileDater.DateType.Created) Then
Me._File.CreationTime = Me._PreservedCreationDate
End If
' Accessed
If Me._PreservedTypes.HasFlag(FileDater.DateType.Accessed) Then
Me._File.LastAccessTime = Me._PreservedLastAccessDate
End If
' Modified
If Me._PreservedTypes.HasFlag(FileDater.DateType.Modified) Then
Me._File.LastWriteTime = Me._PreservedLastModifyDate
End If
End Sub
''' <summary>
''' Sets the specified dates on the file.
''' Note:
''' Calling this method does not cause the deletion of any preserved date.
''' After setting a date, must call once the <see cref="Preserve"/> method if want to preserve any new date established.
''' </summary>
''' <param name="DateType">Indicates the type of dates to set on the file.</param>
''' <param name="Date">Indicates the date.</param>
Public Sub [Set](ByVal DateType As DateType, ByVal [Date] As Date)
Me.DisposedCheck()
' Creation
If DateType.HasFlag(FileDater.DateType.Created) Then
Me._File.CreationTime = [Date]
End If
' Accessed
If DateType.HasFlag(FileDater.DateType.Accessed) Then
Me._File.LastAccessTime = [Date]
End If
' Modified
If DateType.HasFlag(FileDater.DateType.Modified) Then
Me._File.LastWriteTime = [Date]
End If
End Sub
''' <summary>
''' Sets at once all the dates on the file.
''' Note:
''' Calling this method does not cause the deletion of any preserved date.
''' After setting a date, must call once the <see cref="Preserve"/> method if want to preserve any new date established.
''' </summary>
''' <param name="Date">Indicates the date.</param>
Public Sub [Set](ByVal [Date] As Date)
Me.DisposedCheck()
Me._File.CreationTime = [Date]
Me._File.LastAccessTime = [Date]
Me._File.LastWriteTime = [Date]
End Sub
#End Region
#Region " IDisposable "
''' <summary>
''' To detect redundant calls when disposing.
''' </summary>
Private IsDisposed As Boolean = False
''' <summary>
''' Prevent calls to methods after disposing.
''' </summary>
''' <exception cref="System.ObjectDisposedException"></exception>
Private Sub DisposedCheck()
If Me.IsDisposed Then
Throw New ObjectDisposedException(Me.GetType().FullName)
End If
End Sub
''' <summary>
''' Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
''' </summary>
Public Sub Dispose() Implements IDisposable.Dispose
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
''' <summary>
''' Releases unmanaged and - optionally - managed resources.
''' </summary>
''' <param name="IsDisposing">
''' <c>true</c> to release both managed and unmanaged resources;
''' <c>false</c> to release only unmanaged resources.
''' </param>
Protected Sub Dispose(ByVal IsDisposing As Boolean)
If Not Me.IsDisposed Then
If IsDisposing Then
InstancedFiles.Remove(Me._File)
End If
End If
Me.IsDisposed = True
End Sub
#End Region
End Class
#End Region