ASP.NET File Manager - Create A Custom File System Provider (available now in v2011.1)

ASP.NET Team Blog
09 March 2011

Extend the DevExpress ASP.NET File Manager control with your own custom file system provider.

The DXperience v2011 volume 1 release is available now and you can create your own file system provider on top of the existing File System Provider.

ASP.NET File Manager

We added the ability to data bind the ASPxFileManager using datasources. And you can also extend and use a custom file system provider.

Read on to learn how to create your own file system provider to use with our ASP.NET File Manager control.

Default File System Providers

First, some background information: A File System Provider is an API to access to the virtual file system in the ASPxFileManager control.

FileSystemProviderBase_ClassThis API provides the definition of methods for getting a file and folder hierarchy. We also provide the methods for editing the FileManager's items like creating a folder, renaming files/folders, deleting files/folders, etc.

The File Manager control uses the provider for all operations in the file system. The ASPxFileManager has two predefined providers: PhysicalFileSystemProvider and DataSourceFileSystemProvider. These are providers created automatically inside the FileManager control based on chosen properties.

Custom File System Providers

To create your own custom provider, simply derive from a common class and override a few methods. Here are the 3 overall steps:

1. Create a descendant of the FileSystemProviderBase class or one of the predefined provider classes.

The class FileSystemProviderBase includes all the methods for working with the file system:

  • Most common methods: GetFiles and GetFolders, that returns list of files/folders for the specified parent
  • Editing methods: CreateFolder, RenameFolder, RenameFile, etc., used for performing operations with file system items
  • ReadFile - a method, used for getting a file Stream (used by the FileManager for creating a file's thumbnails and downloading)

2. Override those methods that are required for the task at hand. You don't need to override all the methods if you descend from another predefined provider.

For example, you can to override ReadFile method in DataSourceFileSystemProvider to get file content from storage outside the current datasource:

public class CustomHybridFileSystemProvider : DataSourceFileSystemProvider
{
    const string StoragePath = "D:\\FilesStorage";
 
    public CustomHybridFileSystemProvider(string rootFolder)
        :base(rootFolder) { }
 
    public override System.IO.Stream ReadFile(FileManagerFile file) {
        string filePath = GetPhysicalFilePath(file.RelativeName);
        return File.Exists(filePath) ? File.OpenRead(filePath) : Stream.Null;
    }
 
    string GetPhysicalFilePath(string relativeName) {
        return Path.Combine(StoragePath, relativeName);
    }
}

Therefore, you can do either of the following when creating your custom file provider:

  • Create a FileSystemProviderBase descendant and override all of its methods related to getting a file system hierarchy, and use only the relevant methods that are required for editing
  • Create a descendant of any of the predefined providers  and override only those methods that are required. For example, you can to override ReadFile method in DataSourceFileSystemProvider to get file content from storage outside the current datasource

3. Finally, tell the ASPxFileManager control which provider to use. Set the:

  • ASPxFileManager.SettingsDataSource.DataSourceID or ASPxFileManager.SettingsDataSource.DataSource properties, and the ASPxFileManager will create an instance of DataSourceFileSystemProvider. Otherwise, the ASPxFileManager will work with the standard physical file system provider
  • ASPxFileManager.CustomFileSystemProvider or ASPxFileManager.CustomFileSystemProviderTypeName properties, and the ASPxFileManager will use your custom provider instead of the default one
<dx:ASPxFileManager 
    ID="ASPxFileManager1" 
    runat="server" 
    CustomFileSystemProviderTypeName="WebApplication1.CustomHybridFileSystemProvider" 
    DataSourceID="DataSource1" >
    <Settings ThumbnailFolder="~\Thumb\" />
    <SettingsDataSource 
        KeyFieldName="Id" 
        ParentKeyFieldName="Pid" 
        NameFieldName="Name" 
        IsFolderFieldName="IsFolder" 
        LastWriteTimeFieldName="LastWriteTime" />
</dx:ASPxFileManager>

Available now in v2011 volume 1

The new file system providers in the DevExpress ASP.NET File Manager control are powerful and extensible.

And this new custom file system provider is available now in the DXperience v2011 volume 1 release.

What do you think of the new capability to create your own customer file system provider? Drop me a line below. Thanks!

Follow MehulHarry on Twitter

DXperience? What's That?

DXperience is the .NET developer's secret weapon. Get full access to a complete suite of professional components that let you instantly drop in new features, designer styles and fast performance for your applications. Try a fully-functional version of DXperience for free now: http://www.devexpress.com/Downloads/NET/

Free DevExpress Products - Get Your Copy Today

The following free DevExpress product offers remain available. Should you have any questions about the free offers below, please submit a ticket via the DevExpress Support Center at your convenience. We'll be happy to follow-up.
No Comments

Please login or register to post comments.