Root Folder

  • 4 minutes to read

Specify a UNC Path to the Root Folder

The ASPxFileManager allows you to specify a UNC path to the root folder in the following ways:

  • Path to a folder in the web application

    <dx:ASPxFileManager ID="ASPxFileManager1" runat="server">
         <Settings RootFolder="~\folder" ThumbnailFolder="~\Thumb\" />
    </dx:ASPxFileManager>
    
  • Path to a network folder

    <dx:ASPxFileManager ID="ASPxFileManager1" runat="server">
         <Settings RootFolder="\\resource\folder" ThumbnailFolder="~\Thumb\" />
    </dx:ASPxFileManager>
    
  • Relative path to a folder

    You can specify a relative path to the current site's folder.

    <dx:ASPxFileManager ID="ASPxFileManager1" runat="server">
         <Settings RootFolder="..\..\ProjectData\folder" ThumbnailFolder="~\Thumb\" />
    </dx:ASPxFileManager>
    

    Specify a relative path to the database root node if the ASPxFileManager gets content from a database.

    <dx:ASPxFileManager ID="ASPxFileManager1" runat="server" DataSourceID="XpoDataSource1">
            <Settings ThumbnailFolder="~\Thumb\" RootFolder="resource\folder" />
            <SettingsDataSource KeyFieldName="Id" ParentKeyFieldName="Pid" NameFieldName="Name" IsFolderFieldName="IsFolder" FileBinaryContentFieldName="Data" LastWriteTimeFieldName="LastWriteTime" />
        </dx:ASPxFileManager>
    
  • Absolute path to a folder on a local disc

    <dx:ASPxFileManager ID="ASPxFileManager1" runat="server">
         <Settings RootFolder="c:\folder" ThumbnailFolder="~\Thumb\" />
    </dx:ASPxFileManager>
    

    The ASPxFileManager builds a tree view for the entire folder hierarchy. Use the folder's virtual mode if you set a local disc as the root folder (for example, RootFolder="c:"). In virtual mode (FileManagerSettingsFolders.EnableCallBacks), the file manager retrieves data from the server in portions to reduce the server load and start-up time for complex hierarchies.

    <dx:ASPxFileManager ID="ASPxFileManager1" runat="server">
         <Settings RootFolder="c:\" ThumbnailFolder="~\Thumb\" />
         <SettingsFolders EnableCallBacks="True" />
    </dx:ASPxFileManager>
    
NOTE

The root folder cannot be changed during its own callbacks. Instead, use postbacks or the ASPxCallbackPanel's callbacks.

Display an FTP Folder within the File Manager

Create a custom file system provider (PhysicalFileSystemProvider) to use FTP with the ASPxFileManager.

The following example illustrates how to override the FileSystemProviderBase.GetFiles and the FileSystemProviderBase.GetFolders functions to display files and folders from an FTP server:

NOTE

The following example does not allow you to download or upload files. You can implement your own algorithms to extend the class with these functions.

<dx:ASPxFileManager ID="ASPxFileManager1" runat="server" CustomFileSystemProviderTypeName="MySite.CustomProvider">
     <Settings RootFolder="/" ThumbnailFolder="~\Thumb"/>
</dx:ASPxFileManager>
namespace MySite {
...    
    enum ElementType
    {
        Directory,
        File
    }

    public class CustomProvider : FileSystemProviderBase {
        private String _connectionString;
        private NetworkCredential _creds;

        public CustomProvider(String folder) : base(folder) 
        {
            var ftpUser = "username";
            var ftpPass = "password";
            var ftpServer = "127.0.0.1";
            var dir = "Corrector";
            _creds = new NetworkCredential(ftpUser, ftpPass);
            _connectionString = String.Format("ftp://{0}/{1}", ftpServer, dir);
        }

        public CustomProvider(String connectionString, NetworkCredential creds, String folder)
            : base(folder) {
            _connectionString = connectionString;
            _creds = creds;
        }

        public override bool Exists(FileManagerFile file) {
            return base.Exists(file);
        }

        public override bool Exists(FileManagerFolder folder) {
            return base.Exists(folder);
        }

        List<String> FindElements(String fullname, ElementType type) {
            var result = new List<String>();
            var uri = new Uri(String.Format("{0}{1}/", _connectionString, fullname));
            var request = (FtpWebRequest)WebRequest.Create(uri);
            request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
            request.Credentials = _creds;
            request.UsePassive = false;
            request.KeepAlive = true;
            var response = (FtpWebResponse)request.GetResponse();
            var responseStream = response.GetResponseStream();
            var reader = new StreamReader(responseStream);
            var lstdirectory = reader.ReadLine();
            while(lstdirectory != null) {
                var dirDetails = lstdirectory.Split(new char[] { ' ' }, 9, StringSplitOptions.RemoveEmptyEntries);
                var dirFlag = dirDetails[0].Substring(0, 1);
                var filename = dirDetails[8];
                if((type == ElementType.File && !dirFlag.Equals("d"))
                    || (type == ElementType.Directory && dirFlag.Equals("d"))) {
                    result.Add(filename);
                }
                lstdirectory = reader.ReadLine();
            }
            return result;
        }

        public override IEnumerable<FileManagerFile> GetFiles(FileManagerFolder folder) {
            var files = FindElements(folder.FullName, ElementType.File);
            return files.Select(file => new FileManagerFile(this, file)).ToArray();
        }

        public override IEnumerable<FileManagerFolder> GetFolders(FileManagerFolder parentFolder) {
            var directories = FindElements(parentFolder.FullName, ElementType.Directory);
            return directories.Select(directory => new FileManagerFolder(this, directory)).ToArray();
        }
    }
}