Xceed .NET Libraries Documentation
Welcome to Xceed Data Manipulation Compoents for .NET and .NET Standard / Advanced Concepts / Extending / Extending the AbstractFile class

In This Topic
    Extending the AbstractFile class
    In This Topic

    The AbstractFile class, as well as all the classes that derive from the AbstractFile class, can be derived from to create custom file classes.

    Basic Steps

    When creating a class that derives from the AbstractFile class or one of its derived classes, the following functions must be implemented: 

    Method/Property Description
    DoSize Gets the size of the file in bytes.
    DoOpenRead Opens the file for reading.
    DoOpenWrite Opens the file for writing.
    DoName Gets or sets the short name of the item.
    DoFullName Gets the full name of the item.
    DoAttributes Gets or sets the attributes of the item. If your custom file does not support attributes, override the DoHasAttributes property and return false. This will prevent the DoAttributes method from being called needlessly.
    DoCreationDateTime Gets or sets the creation date and time of the item. If your custom file does not support having a creation date and time, override the DoHasCreationDateTime property and return false. This will prevent the DoCreationDateTime method from being called needlessly.
    DoLastWriteDateTime Gets or sets the modification date and time of the item. If your custom file does not support having a last write date and time, override the DoHasLastWriteDateTime property and return false. This will prevent the DoLastWriteDateTime method from being called needlessly.
    DoLastAccessDateTime Gets or sets the last access date and time of the item. If your custom file does not support having a last access date and time, override the DoHasLastAccessDateTime property and return false. This will prevent the DoLastAccessDateTime method from being called needlessly.
    DoParentFolder Gets a reference to the parent folder of this item.
    DoRootFolder Gets a reference to the root folder of this item.
    DoExists Gets a boolean value indicating if the item physically exists.
    DoRefresh Re-reads the information from the physical item.
    DoCreate Creates the physical item represented by the FileSystemItem object.
    DoDelete Permanently deletes the physical item.
    IsSameAs Gets a boolean value indicating if the source and target items are the same.
    IsPathRooted Returns a boolean value indicating if the path passed is rooted in the environment of the FileSystemItem object we are dealing with.

    Each of the overridden abstract "Do" methods and properties are called by their corresponding public counterparts and are responsible for executing the actual operation. The public method simply validates the parameters. It is the "Do" implementation that must make sure that the required conditions are met. For example, in order to call delete on an item, it must first exist. This means that your implementation of DoDelete must throw an ItemDoesNotExistException if the item does not exist.

    Template

    The following example demonstrates the minimum implementation required for a class that derives from the AbstractFile class.

    VB.NET Copy Code

    Imports Xceed.FileSystem
    Imports System.IO

    Namespace Xceed.FileSystem.Samples
       Public Class CustomFile
          Inherits AbstractFile
     
          Protected Overrides ReadOnly Property DoSize() As Long
             Get
                Return 0
             End Get
           End Property    

          Protected Overrides Function DoOpenRead(ByVal session As FileSystemEventsSession, _
                                                  ByVal share As FileShare) As Stream      
             Return Nothing
          End Function    

          Protected Overrides Function DoOpenWrite(ByVal session As FileSystemEventsSession, _
                                                   ByVal overwrite As Boolean, _
                                                   ByVal share As FileShare) As Stream
             Return Nothing
          End Function

          Protected Overrides Property DoName() As String
             Get       
                Return Nothing
             End Get
             Set(ByVal Value As String)
             End Set   
          End Property    

          Protected Overrides ReadOnly Property DoFullName() As String
             Get       
                Return Nothing
             End Get
          End Property
     
          Protected Overrides Property DoAttributes() As FileAttributes
             Get
                Return New FileAttributes()
             End Get
             Set(ByVal Value As FileAttributes)
             End Set
          End Property

          Protected Overrides Property DoCreationDateTime() As DateTime
             Get
                Return New System.DateTime()
             End Get
             Set(ByVal Value As DateTime)
             End Set
          End Property

          Protected Overrides Property DoLastWriteDateTime() As DateTime
             Get
                Return New System.DateTime()
             End Get
             Set(ByVal Value As DateTime)
             End Set
          End Property

          Protected Overrides Property DoLastAccessDateTime() As DateTime
             Get
                Return New System.DateTime()
             End Get
             Set(ByVal Value As DateTime)
             End Set
          End Property

          Protected Overrides ReadOnly Property DoParentFolder() As AbstractFolder
             Get
                Return Nothing
             End Get
          End Property
     
          Protected Overrides ReadOnly Property DoRootFolder() As AbstractFolder
             Get
                Return Nothing
             End Get
          End Property

          Protected Overrides ReadOnly Property DoExists() As Boolean
             Get
                Return True
             End Get
           End Property
     
          Protected Overrides Sub DoRefresh(ByVal session As FileSystemEventsSession)
          End Sub    

          Protected Overrides Sub DoCreate(ByVal session As FileSystemEventsSession)
          End Sub    

          Protected Overrides Sub DoDelete(ByVal session As FileSystemEventsSession)
          End Sub    

          Protected Overrides Function IsSameAs(ByVal target As FileSystemItem) As Boolean
             Return True
          End Function

          Protected Overrides Function IsPathRooted(ByVal path As String) As Boolean
             Return True
          End Function
       End Class
    End Namespace

    C# Copy Code

    using System;
    using System.IO;
    using Xceed.FileSystem;

    namespace Xceed.FileSystem.Samples
    {
      public class CustomFile : AbstractFile
      {
        public CustomFile()
        {
        }

        protected override long DoSize
        {
          get { return 0; }
        }

        protected override Stream DoOpenRead(FileSystemEventsSession session, FileShare share)
        {
          return null;
        }

        protected override Stream DoOpenWrite(FileSystemEventsSession session, bool overwrite, FileShare share)
        {
          return null;
        }

        protected override string DoName
        {
          get { return null; }
          set { }
        }

        protected override string DoFullName
        {
          get { return null; }
        }

        protected override FileAttributes DoAttributes
        {
          get { return new FileAttributes(); }
          set { }
        }

        protected override DateTime DoCreationDateTime
        {
          get { return new DateTime(); }
          set { }
        }

        protected override DateTime DoLastWriteDateTime
        {
          get { return new DateTime(); }
          set { }
        }

        protected override DateTime DoLastAccessDateTime
        {
          get { return new DateTime(); }
          set { }
        }

        protected override AbstractFolder DoParentFolder
        {
          get { return null; }
        }

        protected override AbstractFolder DoRootFolder
        {
          get { return null; }
        }

        protected override bool DoExists
        {
          get { return true; }
        }

        protected override void DoRefresh(FileSystemEventsSession session)
        {

        }

        protected override void DoCreate(FileSystemEventsSession session)
        {

        }

        protected override void DoDelete(FileSystemEventsSession session)
        {

        }

        protected override bool IsSameAs(FileSystemItem target)
        {
          return true;
        }

        protected override bool IsPathRooted(string path)
        {
          return true;
        }
      }
    }