The original file path is fileoldpath;
New file path: filenewpath,
Then you can use
File. Move (fileoldpath, filenewpath)
Or file. Copy (fileoldpath, filenewpath)
Note that the path here is the folder path + file name, which can be implemented using path. Combine ().
C # copy a folder
The Directory class contains the createdirectory, move, and delete methods, but there is no copy method-_-. Below we will write a class for implementation, the idea is to recursively copy the files in the specified folder and Its subfolders to the target Folder:
Program Code
Using system;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. IO;
/// <Summary>
/// Summary of mydirectory
/// </Summary>
Public class mydirectory
{
/// <Summary>
/// Copy a folder
/// </Summary>
/// <Param name = "sourcedirname"> original path </param>
/// <Param name = "destdirname"> target path </param>
/// <Returns> </returns>
Public static void copy (string sourcedirname, string destdirname)
{
If (sourcedirname. substring (sourcedirname. Length-1 )! = "\\")
{
Sourcedirname = sourcedirname + "\\";
}
If (destdirname. substring (destdirname. Length-1 )! = "\\")
{
Destdirname = destdirname + "\\";
}
If (directory. exists (sourcedirname ))
{
If (! Directory. exists (destdirname ))
{
Directory. createdirectory (destdirname );
}
Foreach (string item in directory. getfiles (sourcedirname ))
{
File. Copy (item, destdirname + path. getfilename (item), true );
}
Foreach (string item in directory. getdirectories (sourcedirname ))
{
Copy (item, destdirname + item. substring (item. lastindexof ("\") + 1 ));
}
}
}
}