Search code examples
asp.netasp.net-3.5

Copy file from one folder to another folder


I am working on website in which i want to copy the file from my application folder to other folder on same server (But this folder is out of my application folder i.e. my application on C driver and the destination folder is on D drive).Is this possible using any functionality of Asp.Net?

Thanks in advance.


Solution

  • YES it's possible, the only concern that you have to watch for is that the CopyTo path should be the full path, not the relative one (ex: c:\websites\myOtherFolder).

    this way, you can successfully copy/move the file from your ASP.NET code.

    below is a pseudo code to show you how to get it done (assuming that the file has been placed on the root folder of your ASP.NET Application).

     using System.IO;
        ..
        ..
        ..
    
    
    
    // Get the current app path:
    var currentApplicationPath =  HttpContext.Current.Request.PhysicalApplicationPath;
    
    //Get the full path of the file    
    var fullFilePath = currentApplicationPath + fileNameWithExtension;
    
    // Get the destination path
    var copyToPath = "This has to be the full path to your destination directory. 
                      Example d:\myfolder";
    
    // Copy the file
    File.Copy(fullFilePath , copyToPath );