C#复制文件到指定文件夹

C#复制文件到指定文件夹

2010-06-04  16:09

如: 路径:“D:/a/b” 有如下文件 P1009.txt Z1009.txt T1009.txt 复制到 E:盘下,自动建立一个以日期为文件夹名的文件夹下。

可以使用Process,用它打开CMD,然后执行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) {//实现从一个目录下完整拷贝到另一个目录下。 Directory.CreateDirectory(varToDirectory); if(!Directory.Exists(varFromDirectory))    { m_eorrStr    =    "对不起,您要拷贝的目录不存在。 "; return; }

string[]    directories    =    Directory.GetDirectories(varFromDirectory);//取文件夹下所有文件夹名,放入数组;                          if(directories.Length    >    0) { foreach(string    d    in    directories) { CopyFiles(d,varToDirectory    +    d.Substring(d.LastIndexOf( "// ")));//递归拷贝文件和文件夹 } }                          string[]    files    =    Directory.GetFiles(varFromDirectory);//取文件夹下所有文件名,放入数组;                          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 ");     } }

posted @ 2012-06-12 21:31  天黑了  阅读(2276)  评论(0编辑  收藏  举报