将文件从一个文件夹复制到另一个 [英] copying files from one folder to another
本文介绍了将文件从一个文件夹复制到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在文件夹中有一组文本文件。我想将这个文件夹复制到另一个名称和数据时间的文件夹中。
Hi,
I have set of text files in folder. I want to copy this folders into another folder with name and datatime.
string directory = @"C:\Documents and Settings\vinodhini\Desktop\Merge Data\SampleData";
string comdir = @"C:\Documents and Settings\vinodhini\Desktop\Merge Data\CompletedData";
string[] fiels = System.IO.Directory.GetFiles(directory);
foreach (string s in fiels)
{
string st = DateTime.Now.ToShortDateString();
string fileName = System.IO.Path.GetFileName(s)+st;
string destFile = System.IO.Path.Combine(comdir, fileName);
System.IO.File.Copy(s, destFile, true);
}
但我得到不支持给定路径的格式这个错误。
有人帮我解决这个问题吗?
But i got "The given path's format is not supported" this error.
Anyone help me to solve this?
推荐答案
尝试使用这个...
try using this...
string st = Date.Now.Year + Date.Now.Month + Date.Now.Day //outputs the date without "/"
我在VB.Net中尝试过。它运作良好。
I tried it in VB.Net. It works well.
嗯......已经做了很小的改动:
Hmmm... Small changes have been done:
string directory = @"C:\Documents and Settings\vinodhini\Desktop\Merge Data\SampleData";
string comdir = @"C:\Documents and Settings\vinodhini\Desktop\Merge Data\CompletedData\" + DateTime.Now.ToString("yyyyMMdd") + "\";
string[] fiels = System.IO.Directory.GetFiles(directory);
foreach (string s in fiels)
{
string fileName = System.IO.Path.GetFileName(s);
string destFile = System.IO.Path.Combine(comdir, fileName);
System.IO.File.Copy(s, destFile, true);
}
谢谢你我解决了自己
Thank you i solve myself
string dt = DateTime.Now.ToString("dd-MM-yyyy-hh-mm");
这篇关于将文件从一个文件夹复制到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文