logo资料库

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

第1页 / 共4页
第2页 / 共4页
第3页 / 共4页
第4页 / 共4页
资料共4页,全文预览结束
C#复制文件到指定文件夹 2010-06-04 16:09 如: 路径:“D:\a\b” 有如下文件 P1009.txt Z1009.txt T1009.txt 复制到 E:盘下,自动建立一个以日期为文件夹名的文件夹下。 可以使用 Process,用它打开 CMD,然后执行 DOS 命令!(可以设置不打开 CMD 窗口) 《1》 = p System.Diagnostics.Process p.StartInfo.FileName= "cmd.exe "; p.StartInfo.UseShellExecute=false; p.StartInfo.RedirectStandardInput=true; p.StartInfo.RedirectStandardOutput = p.StartInfo.RedirectStandardError = p.StartInfo.CreateNoWindow = true; true; new System.Diagnostics.Process(); true; c:\abc\*.* b:\abc "); p.Start(); p.StandardInput.WriteLine( "copy p.StandardInput.WriteLine( "exit "); p.StandardOutput.ReadToEnd(); p.Close(); 《2》 varFromDirectory,string varToDirectory) void CopyFiles(string public {//实现从一个目录下完整拷贝到另一个目录下。 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) files) + s.Substring(s.LastIndexOf( "\\ "))); s in { foreach(string { File.Copy(s,varToDirectory } } } 《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) { p string path ="copy "+ sourceFile + " " + destFile; System.Diagnostics.Process p.StartInfo.FileName= "cmd.exe "; p.StartInfo.UseShellExecute=false; p.StartInfo.RedirectStandardInput=true; p.StartInfo.RedirectStandardOutput = p.StartInfo.RedirectStandardError = p.StartInfo.CreateNoWindow = true; true; = new System.Diagnostics.Process(); 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 "); } }
本 文 来 自 CSDN 博 客 , 转 载 请 标 明 出 处 : http://blog.csdn.net/ou_yangpengfei/archive/2010/09/10/5874823.aspx 本 文 来 自 CSDN 博 客 , 转 载 请 标 明 出 处 : http://blog.csdn.net/ou_yangpengfei/archive/2010/09/10/5874823.aspx
分享到:
收藏