Copy a file from one folder to another folder

I am working on a website on which I want to copy a file from the folder of my application to another folder on the same server (but this folder is outside my application folder, i.e. my application is on the C-driver and the destination folder is on disk D) Is this possible using any Asp.Net feature?

Thanks in advance.

+3
source share
3 answers

YES, this is possible, the only thing you should observe is that the CopyTo path should be complete, not relative (ex: c: \ websites \ myOtherFolder ).

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

- , , (, ASP.NET).

 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 );
+8

:

System.IO.File.Copy(FileToCopy, NewCopy)
0

It is very easy to move a file from one folder to another folder. you can change the file name while moving ...

           string Tranfiles, ProcessedFiles;

           //Tranfiles = Server.MapPath(@"~\godurian\sth100\transfiles\" + Filename);

           Tranfiles = Server.MapPath(@"~\transfiles\" + Filename);
           if (File.Exists(Server.MapPath(@"~\transfiles\" + Filename)))
           {
               File.Delete(Server.MapPath(@"~\transfiles\" + Filename));
           }

           //ProcessedFiles = Server.MapPath(@"~\godurian\sth100\ProcessedFiles");
           ProcessedFiles = Server.MapPath(@"~\ProcessedFiles");

           File.Move(Tranfiles, ProcessedFiles);

Now you can check the folder of your application to confirm the status of the move process

0
source

All Articles