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) name description Attributes Gets or sets the FileAttributes of the current FileSystemInfo. CreationTime Gets or sets the creation time of the current FileSystemInfo object Exists Gets a value indicating whether the file or directory exists Extension Gets the string representing the extension part of the file. FullName Gets the full path of the directory or file. LastAccessTime Gets or sets the time the current file or directory was last accessed LastWriteTime Gets or sets the time when the current file or directory was last written to. Name For files, gets the name of the file. For directories, gets the name of the last directory in the hierarchy if a hierarchy exists. Otherwise, the Name property gets the name of the directory Delete Deletes a file or directory. FullPath Represents the fully qualified path of the directory or file. OriginalPath The path originally specified by the user, whether relative or absolute. 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. name description Directory Gets an instance of the parent directory. DirectoryName Gets a string representing the directory's full path. Length Gets the size of the current file or directory. Name Gets the name of the file. Create Creates a file. CreateText Creates a StreamWriter that writes a new text file. Open Opens a file with various read/write and sharing privileges OpenRead Creates a read-only FileStream. OpenText Creates a StreamReader with UTF8 encoding that reads from an existing text file. OpenWrite Creates a write-only FileStream AppendText Creates a StreamWriter that appends text to the file represented by this instance of the FileInfo. CopyTo Copies an existing file to a new file. MoveTo Renames/moves an existing file. 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. name description Parent Gets the parent directory of a specified subdirectory. Root Gets the root portion of a path. Create Creates a directory. CreateSubDirectory Creates a subdirectory or subdirectories on the specified path. The specified path can be relative to this instance of the DirectoryInfo class. GetDirectories Returns the subdirectories of the current directory GetFiles Returns a file list from the current directory. GetFileSystemInfos Retrieves an array of strongly typed FileSystemInfo objects. MoveTo Moves a DirectoryInfo instance and its contents to a new path. 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;using System.IO; public class CFileSystem:FileSystemInfo { FileInfo mobjFileInfo; DirectoryInfo mobjDirectoryInfo; bool mblnIsDirectory = true; public CFileSystem(string xstrFilePath) { try { mobjDirectoryInfo = new DirectoryInfo(xstrFilePath); if (!(mobjDirectoryInfo.Extension.Length==0)) { mblnIsDirectory = false; mobjDirectoryInfo= null; mobjFileInfo = new FileInfo(xstrFilePath); } } catch (Exception ex) { Console.WriteLine( ex.ToString()); } } public override void Delete() { if (mblnIsDirectory) { mobjDirectoryInfo.Delete(); } else { mobjFileInfo.Delete(); } } public override bool Exists { get { if (mblnIsDirectory) { return mobjDirectoryInfo.Exists; } else { return mobjFileInfo.Exists; } } } public string Fullname { get { if (mblnIsDirectory) { return mobjDirectoryInfo.FullName; } else { return mobjFileInfo.FullName; } } } public override string Name { get { if (mblnIsDirectory) { return mobjDirectoryInfo.Name; } else { return mobjFileInfo.Name; } } } public bool IsDirectory { get { return mblnIsDirectory; } } public bool IsFile { get { return !mblnIsDirectory; } }} 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

评论