Improved copy/paste support in RadFileExplorer

4 posts, 0 answers
  1. Martin
    Martin avatar
    199 posts
    Member since:
    Oct 2005

    Posted 08 Dec 2009 Link to this post

    Requirements

    RadControls version

    2009/Q2 or newer
    .NET version

    3.5
    Visual Studio version

    2008
    programming language

    C#
    browser support

    all browsers supported by RadControls


    PROJECT DESCRIPTION
    The attached custom file-system content provider for the RadFileExplorer control improves the copy/paste operations of the RadFileExplorer control. By using this content provider instead of the default one, it is possible to create a copy of an existing file in the same folder.

    Example:
    • copy an existing file, e.g. "default.htm"
    • select the paste command --> a copy of the file will be created with the name "default [1].htm"
    • copy/paste "default.htm" again --> another copy named "default [2].htm" will be created

    Installation / Usage:
    • (see attached sample for a demo)
    • copy the attached class into your project (e.g. into the app_code folder of a web site project)
    • on every page/control where you are using the RadFileExplorer control, replace the default content provider with the attached one, e.g. using the following line of code in the Init/Load event handler:
    protected void Page_Load(object sender, EventArgs e) 
        RadFileExplorer1.Configuration.ContentProviderTypeName = 
            typeof(Telerik.Utilities.ImprovedFileExplorerContentProvider).AssemblyQualifiedName; 
        } 
     
  2. Fiko
    Admin
    Fiko avatar
    1406 posts

    Posted 11 Dec 2009 Link to this post

    Hi Martin,

    I tested the provided code and it works fine when a file is copied to the same folder where the source file is. For instance, you can successfully copy a file with name File.jpg from FolderA to the same folder (FolderA) and the result will be File[1].jpg. If you paste the file, however, to a folder different than FolderA the default message "A file with a same name as the target already exists! " appears and the operation is aborted.

    Your idea is great and if you rework the project to cover all scenarios, we will gladly award you with 1000 Telerik points for your effort.

    Regards,
    Fiko
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. Martin
    Martin avatar
    199 posts
    Member since:
    Oct 2005

    Posted 11 Dec 2009 Link to this post

    Hi Fiko,
    thanks for the feedback. Please find in the attachment an updated version, which also handles the described case correctly.

    Best regards,
    Martin
  4. Fiko
    Admin
    Fiko avatar
    1406 posts

    Posted 16 Dec 2009 Link to this post

    Hi Martin,

    The provided code works correctly, but I made some changes in it in order to use the built in methods in the VirtualUtility class.

    The reworked code looks like this:
     

    public override string CopyFile(string sourcePath, string newPath)
    {
        if (FileExists(sourcePath) && FileExists(newPath))
        {
            // The original approach:
            //var path = newPath.Substring(0, newPath.LastIndexOf("/") + 1);
            //var name = newPath.Substring(newPath.LastIndexOf("/") + 1);
     
            // TELERIK's solution:
            string path = VirtualPathUtility.GetDirectory(newPath);// gets the directory
            string name = VirtualPathUtility.GetFileName(newPath);
            var extension = VirtualPathUtility.GetExtension(newPath);
     
            // The original approach (tis code is not necessory) :
            //if (name.IndexOf(".") >= 0)
            //{
            //    extension = name.Substring(name.LastIndexOf("."));
            //    name = name.Substring(0, name.LastIndexOf("."));
            //}
     
            var fileNumber = 0;
            while (FileExists(newPath))
            {
                newPath = string.Format("{0}{1} [{2}]{3}", path, name, ++fileNumber, extension);
            }
        }
        return base.CopyFile(sourcePath, newPath);
    }

    The idea is excellent and I believe that it will be useful for the Telerik community. Your Telerik points are updated.

    Greetings,
    Fiko
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Back to Top