C#复制文件到指定文件夹
C#復制文件到指定文件夾
2010-06-04? 16:09如: 路徑:“D:/a/b” 有如下文件 P1009.txt Z1009.txt T1009.txt 復制到 E:盤下,自動建立一個以日期為文件夾名的文件夾下。
可以使用Process,用它打開CMD,然后執(zhí)行DOS命令!(可以設置不打開CMD窗口)
《1》
System.Diagnostics.Process??? p??? =??? new??? System.Diagnostics.Process(); p.StartInfo.FileName= "cmd.exe "; p.StartInfo.UseShellExecute=false; p.StartInfo.RedirectStandardInput=true; p.StartInfo.RedirectStandardOutput??? =??? true;????? p.StartInfo.RedirectStandardError??? =??? true;????? p.StartInfo.CreateNoWindow??? =??? true;?
p.Start();????? p.StandardInput.WriteLine( "copy??? c:/abc/*.*??? b:/abc ");????? p.StandardInput.WriteLine( "exit ");????? p.StandardOutput.ReadToEnd();???? p.Close();
《2》
?
public??? void??? CopyFiles(string??? varFromDirectory,string??? varToDirectory) {//實現(xiàn)從一個目錄下完整拷貝到另一個目錄下。 Directory.CreateDirectory(varToDirectory); if(!Directory.Exists(varFromDirectory))??? { m_eorrStr??? =??? "對不起,您要拷貝的目錄不存在。 "; return; }
string[]??? directories??? =??? Directory.GetDirectories(varFromDirectory);//取文件夾下所有文件夾名,放入數(shù)組; ???????????????????????? if(directories.Length??? >??? 0) { foreach(string??? d??? in??? directories) { CopyFiles(d,varToDirectory??? +??? d.Substring(d.LastIndexOf( "// ")));//遞歸拷貝文件和文件夾 } } ???????????????????????? string[]??? files??? =??? Directory.GetFiles(varFromDirectory);//取文件夾下所有文件名,放入數(shù)組; ???????????????????????? if(files.Length??? >??? 0) { foreach(string??? s??? in??? files) { File.Copy(s,varToDirectory??? +??? s.Substring(s.LastIndexOf( "// "))); } } }
《3》
?
?
C# 把文件拷貝到指定文件夾 收藏 if(!System.IO.Directory.Exists(@"C:/text")) { // 目錄不存在,建立目錄 System.IO.Directory.CreateDirectory(@"C:/text"); }
String sourcePath = "c://源文件目錄//原文件名稱.txt";
String targetPath = "d://目標文件目錄//新的文件名稱.aspx";
bool isrewrite=true; // true=覆蓋已存在的同名文件,false則反之
System.IO.File.Copy(sourcePath, targetPath, isrewrite);
?
《4》
C# 把文件拷貝到指定文件夾下面是關鍵代碼:if(!System.IO.Directory.Exists(@"C:/text")) { // 目錄不存在,建立目錄 System.IO.Directory.CreateDirectory(@"C:/text") if(!System.IO.Directory.Exists(@"C:/text")) { // 目錄不存在,建立目錄 System.IO.Directory.CreateDirectory(@"C:/text"); } String sourcePath = "c://源文件目錄//原文件名稱.txt"; String targetPath = "d://目標文件目錄//新的文件名稱.aspx"; bool isrewrite=true; // true=覆蓋已存在的同名文件,false則反之 System.IO.File.Copy(sourcePath, targetPath, isrewrite);
?
?
?
?
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.IO;
public partial class copyfile : System.Web.UI.Page { ???? protected void Page_Load(object sender, EventArgs e) ??? { }
??? public void copyFile(string sourceFile, string destFile) ??? { ??????? string path ="copy "+ sourceFile + " " + destFile; ??????? System.Diagnostics.Process?? p?? =?? new?? System.Diagnostics.Process(); ??????? p.StartInfo.FileName= "cmd.exe "; ??????? p.StartInfo.UseShellExecute=false; ??????? p.StartInfo.RedirectStandardInput=true; ??????? p.StartInfo.RedirectStandardOutput?? =?? true;??? ??????? p.StartInfo.RedirectStandardError?? =?? true;??? ??????? p.StartInfo.CreateNoWindow?? =?? true;?
??????? p.Start();??? ??????? p.StandardInput.WriteLine(path);??? ??????? p.StandardInput.WriteLine( "exit ");??? ??????? p.StandardOutput.ReadToEnd();?? ??????? p.Close(); ??? ??? }
??? protected void Button1_Click(object sender, EventArgs e) ??? { ??????? copyfile c = new copyfile(); ??????? c.copyFile(" c://abc//* "," d://abc "); ??? } }
轉載于:https://www.cnblogs.com/sammon/archive/2012/06/12/2546859.html
總結
以上是生活随笔為你收集整理的C#复制文件到指定文件夹的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: oracleI基础入门(8)--tabl
- 下一篇: 阿里云服务实现逆势增长:Q1收入超176