Class in C# and VB.NET for beginners
FileSystemInfo Class
One of the rich experiences in working with .NET is a huge collection of Base Class Libraries. The .NET Framework class library is a library of classes, interfaces, and value types that are included in the Microsoft .NET Framework SDK. This library provides access to system functionality and is designed to be the foundation on which .NET Framework applications, components, and controls are built.
System.IO namespace contains types that allow synchronous and asynchronous reading and writing on data streams and files.
To parse information of lots of files and directories, the FileSystemInfo class comes in handy. It is an abstract class and contains members (methods, properties …) that are common to file and directory manipulation. A FileSystemInfo object can represent either a file or a directory. The implementation of this abstract class is done in FileInfo and DirectoryInfo class. The Members would take full path (absolute or relative) or UNC path if they require path as one of the parameters.
In VB6 you could have used the Scripting Library (FileSystemObject) to manipulate the Files and directories.
Listing1: Members of FileSystemInfo class (for more information refer to MSDN)
|
The FileInfo class and DirectoryInfo class extends FileSystemInfo class and each add some more members to themselves for specific operations.
FileInfo Class
The FileInfo class provides instance methods for the creation, copying, deletion, moving, and opening of files, and provides members to create FileStream objects.
Listing2: List of some members of FileInfo class (for more information refer to MSDN). I have excluded the members of FileSystemInfo class overridden by FileInfo class and implemented by FileSystemInfo class.
|
DirectoryInfo Class
The DirectoryInfo class provides instance methods for creating, moving, and enumerating through directories and subdirectories.
Listing3: List of some members of DirectoryInfo class (for more information refer to MSDN), I have excluded the members of FileSystemInfo Class overridden by Directory class and implemented by FileSystemInfo class.
|
Below is an example of extending the FileSystemInfo class and adding IsDirectory
and IsFile
properties to it, new members can be added to roll a new custom class rather then using FileInfo or DirectoryInfo class. We can delegate most of the functionality to the FileInfo and DirectoryInfo classes in our custom class.
C# Implementation
using System; |
VB.NET Implementation
Imports System.IO Public Class CFileSystem Inherits System.IO.FileSystemInfo Private mobjFileInfo As FileInfo Private mobjDirectoryInfo As DirectoryInfo Private mblnIsDirectory As Boolean = True Public Sub New(ByVal xstrFilePath As String) Try mobjDirectoryInfo = New DirectoryInfo(xstrFilePath) If Not mobjDirectoryInfo.Extension().Length = 0 Then mblnIsDirectory = False mobjDirectoryInfo = Nothing mobjFileInfo = New FileInfo(xstrFilePath) End If Catch ex As Exception Console.WriteLine(ex.ToString) End Try End Sub Public Overrides Sub Delete() If mblnIsDirectory Then mobjDirectoryInfo.Delete() Else mobjFileInfo.Delete() End If End Sub Public Overrides ReadOnly Property Exists() As Boolean Get If mblnIsDirectory Then Exists = mobjDirectoryInfo.Exists Else Exists = mobjFileInfo.Exists End If End Get End Property Public Overrides ReadOnly Property FullName() As String Get If mblnIsDirectory Then FullName = mobjDirectoryInfo.FullName Else FullName = mobjFileInfo.FullName End If End Get End Property Public Overrides ReadOnly Property Name() As String Get If mblnIsDirectory Then Name = mobjDirectoryInfo.Name Else Name = mobjFileInfo.Name End If End Get End Property Public ReadOnly Property IsDirectory() As Boolean Get IsDirectory = mblnIsDirectory End Get End Property Public ReadOnly Property IsFile() As Boolean Get IsFile = mblnIsDirectory = False End Get End Property End Class |
评论