Back to Devexpress

Root Folder

aspnet-15782-components-file-management-file-manager-concepts-root-folder.md

latest7.5 KB
Original Source

Root Folder

  • Mar 22, 2021
  • 4 minutes to read

Specify a UNC Path to the Root Folder

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

  • Path to a folder in the web application

  • Path to a network folder

  • Relative path to a folder

  • Absolute path to a folder on a local disc

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 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.

aspx
<dx:ASPxFileManager ID="ASPxFileManager1" runat="server" CustomFileSystemProviderTypeName="MySite.CustomProvider">
     <Settings RootFolder="/" ThumbnailFolder="~\Thumb"/>
</dx:ASPxFileManager>
csharp
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();
        }
    }
}
vb
Namespace MySite
Friend ... Enum ElementType
        Directory
        File
End Enum

    Public Class CustomProvider
        Inherits FileSystemProviderBase
        Private _connectionString As String
        Private _creds As NetworkCredential

        Public Sub New(ByVal folder As String)
            MyBase.New(folder)
            Dim ftpUser = "username"
            Dim ftpPass = "password"
            Dim ftpServer = "127.0.0.1"
            Dim dir = "Corrector"
            _creds = New NetworkCredential(ftpUser, ftpPass)
            _connectionString = String.Format("ftp://{0}/{1}", ftpServer, dir)
        End Sub

        Public Sub New(ByVal connectionString As String, ByVal creds As NetworkCredential, ByVal folder As String)
            MyBase.New(folder)
            _connectionString = connectionString
            _creds = creds
        End Sub

        Public Overrides Function Exists(ByVal file As FileManagerFile) As Boolean
            Return MyBase.Exists(file)
        End Function

        Public Overrides Function Exists(ByVal folder As FileManagerFolder) As Boolean
            Return MyBase.Exists(folder)
        End Function

        Private Function FindElements(ByVal fullname As String, ByVal type As ElementType) As List(Of String)
            Dim result = New List(Of String)()
            Dim uri = New Uri(String.Format("{0}{1}/", _connectionString, fullname))
            Dim request = CType(WebRequest.Create(uri), FtpWebRequest)
            request.Method = WebRequestMethods.Ftp.ListDirectoryDetails
            request.Credentials = _creds
            request.UsePassive = False
            request.KeepAlive = True
            Dim response = CType(request.GetResponse(), FtpWebResponse)
            Dim responseStream = response.GetResponseStream()
            Dim reader = New StreamReader(responseStream)
            Dim lstdirectory = reader.ReadLine()
            Do While lstdirectory IsNot Nothing
                Dim dirDetails = lstdirectory.Split(New Char() { " "c }, 9, StringSplitOptions.RemoveEmptyEntries)
                Dim dirFlag = dirDetails(0).Substring(0, 1)
                Dim filename = dirDetails(8)
                If (type = ElementType.File AndAlso (Not dirFlag.Equals("d"))) OrElse (type = ElementType.Directory AndAlso dirFlag.Equals("d")) Then
                    result.Add(filename)
                End If
                lstdirectory = reader.ReadLine()
            Loop
            Return result
        End Function

        Public Overrides Function GetFiles(ByVal folder As FileManagerFolder) As IEnumerable(Of FileManagerFile)
            Dim files = FindElements(folder.FullName, ElementType.File)
            Return files.Select(Function(file) New FileManagerFile(Me, file)).ToArray()
        End Function

        Public Overrides Function GetFolders(ByVal parentFolder As FileManagerFolder) As IEnumerable(Of FileManagerFolder)
            Dim directories = FindElements(parentFolder.FullName, ElementType.Directory)
            Return directories.Select(Function(directory) New FileManagerFolder(Me, directory)).ToArray()
        End Function
    End Class
End Namespace