' ***********************************************************************
' Author : Elektro
' Last Modified On : 08-04-2014
' ***********************************************************************
' <copyright file="MRU.vb" company="Elektro Studios">
' Copyright (c) Elektro Studios. All rights reserved.
' </copyright>
' ***********************************************************************
#Region " Usage Examples "
'Public Class Form1
'
' ' Initialize a new List of MostRecentUsed-Item
' Dim MRUList As New List(Of MRU.Item)
'
' Private Sub Test() Handles MyBase.Shown
'
' ' Add some items into the collection.
' With MRUList
' .Add(New MRU.Item("C:\File1.ext"))
' .Add(New MRU.Item("C:\File2.ext") With {.Date = Date.Today,
' .Icon = Bitmap.FromFile("C:\Image.ico"),
' .Tag = Nothing})
' End With
'
' ' Save the MRUItem collection to local file.
' MRU.IO.Save(MRUList, ".\MRU.tmp")
'
' ' Load the saved collection from local file.
' For Each MRUItem As MRU.Item In MRU.IO.Load(Of List(Of MRU.Item))(".\MRU.tmp")
' MessageBox.Show(MRUItem.FilePath)
' Next MRUItem
'
' ' Just another way to load the collection:
' MRU.IO.Load(MRUList, ".\MRU.tmp")
'
' End Sub
'
'End Class
#End Region
#Region " MostRecentUsed "
''' <summary>
''' Class MRU (MostRecentUsed).
''' Administrates the usage of a MRU item collection.
''' </summary>
Public Class MRU
#Region " Constructors "
''' <summary>
''' Prevents a default instance of the <see cref="MRU"/> class from being created.
''' </summary>
Private Sub New()
End Sub
#End Region
#Region " Types "
#Region "IO"
''' <summary>
''' Performs IO operations with a <see cref="MRU.Item"/> Collection.
''' </summary>
Public Class [IO]
#Region " Constructors "
''' <summary>
''' Prevents a default instance of the <see cref="MRU.IO"/> class from being created.
''' </summary>
Private Sub New()
End Sub
#End Region
#Region " Public Methods "
''' <summary>
''' Saves the specified MRU List to local file, using binary serialization.
''' </summary>
''' <typeparam name="T"></typeparam>
''' <param name="MRUItemCollection">The <see cref="MRU.Item"/> Collection.</param>
''' <param name="filepath">The filepath to save the <see cref="MRU.Item"/> Collection.</param>
Public Shared Sub Save(Of T)(ByVal MRUItemCollection As T,
ByVal filepath As String)
Dim Serializer = New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
' Serialization.
Using Writer As New System.IO.FileStream(filepath, System.IO.FileMode.Create)
Serializer.Serialize(Writer, MRUItemCollection)
End Using ' Writer
End Sub
''' <summary>
''' Loads the specified <see cref="MRU.Item"/> Collection from a local file, using binary deserialization.
''' </summary>
''' <typeparam name="T"></typeparam>
''' <param name="MRUItemCollection">The ByRefered <see cref="MRU.Item"/> collection.</param>
''' <param name="filepath">The filepath to load its <see cref="MRU.Item"/> Collection.</param>
Public Shared Sub Load(Of T)(ByRef MRUItemCollection As T,
ByVal filepath As String)
Dim Serializer = New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
' Deserialization.
Using Reader As New System.IO.FileStream(filepath, System.IO.FileMode.Open)
MRUItemCollection = Serializer.Deserialize(Reader)
End Using ' Reader
End Sub
''' <summary>
''' Loads the specified <see cref="MRU.Item"/> Collection from a local file, using the specified deserialization.
''' </summary>
''' <typeparam name="T"></typeparam>
''' <param name="filepath">The filepath to load its <see cref="MRU.Item"/> Collection.</param>
Public Shared Function Load(Of T)(ByVal filepath As String) As T
Dim Serializer = New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
' Deserialization.
Using Reader As New System.IO.FileStream(filepath, System.IO.FileMode.Open)
Return Serializer.Deserialize(Reader)
End Using ' Reader
End Function
#End Region
End Class
#End Region
#Region " Item "
''' <summary>
''' An Item for a MostRecentUsed-Item collection that stores the item filepath and optionally additional info.
''' This Class can be serialized.
''' </summary>
<Serializable()>
Public Class Item
#Region " Constructors "
''' <summary>
''' Prevents a default instance of the <see cref="MRU.Item"/> class from being created.
''' </summary>
Private Sub New()
End Sub
''' <summary>
''' Initializes a new instance of the <see cref="MRU.Item"/> class.
''' </summary>
''' <param name="FilePath">The item filepath.</param>
''' <exception cref="System.ArgumentNullException">FilePath</exception>
Public Sub New(ByVal FilePath As String)
If FilePath Is Nothing Then
Throw New ArgumentNullException("FilePath")
End If
Me._FilePath = FilePath
End Sub
''' <summary>
''' Initializes a new instance of the <see cref="MRU.Item"/> class.
''' </summary>
''' <param name="File">The fileinfo object.</param>
Public Sub New(ByVal File As System.
IO.
FileInfo)
End Sub
#End Region
#Region " Properties "
''' <summary>
''' Gets the item filepath.
''' </summary>
''' <value>The file path.</value>
Public ReadOnly Property FilePath As String
Get
Return Me._FilePath
End Get
End Property
Private _FilePath As String = String.Empty
''' <summary>
''' Gets the FileInfo object of the item.
''' </summary>
''' <value>The FileInfo object.</value>
Public ReadOnly Property FileInfo As System.IO.FileInfo
Get
Return New System.IO.FileInfo(FilePath)
End Get
End Property
''' <summary>
''' (Optionally) Gets or sets the item last-time open date.
''' </summary>
''' <value>The index.</value>
Public Property [Date] As Date
''' <summary>
''' (Optionally) Gets or sets the item icon.
''' </summary>
''' <value>The icon.</value>
Public Property Icon As Bitmap
''' <summary>
''' (Optionally) Gets or sets the item tag.
''' </summary>
''' <value>The tag object.</value>
Public Property Tag As Object
#End Region
End Class
#End Region
#End Region
End Class
#End Region