
这篇文章主要介绍了C 实现拷贝文件到另一个文件夹下,具有很好的参考价
脚本之家 2023-06-22 15:05:06
(资料图)
////// 拷贝文件到另一个文件夹下 /// /// 源文件路径 /// 目标路径(目标文件夹) public void CopyToFile(string sourceName, string folderPath) { //例子: //源文件路径 //string sourceName = @"D:\Source\Test.txt"; //目标路径:项目下的NewTest文件夹,(如果没有就创建该文件夹) //string folderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "NewTest"); if (!Directory.Exists(folderPath)) { Directory.CreateDirectory(folderPath); } //当前文件如果不用新的文件名,那么就用原文件文件名 string fileName = Path.GetFileName(sourceName); //这里可以给文件换个新名字,如下: //string fileName = string.Format("{0}.{1}", "newFileText", "txt"); //目标整体路径 string targetPath = Path.Combine(folderPath, fileName); //Copy到新文件下 FileInfo file = new FileInfo(sourceName); if (file.Exists) { //true 为覆盖已存在的同名文件,false 为不覆盖 file.CopyTo(targetPath, true); } }
注意方法内注释的用法,可以根据自己的需要更改。
时常我们会遇到文件的复制、上传等问题。特别是自动化生产方面,需要对机台抛出的档案进行搬运、收集,然后对资料里的数据等进行分析,等等。
Winform下,列举集中较常见的档案的搬运。
private void MoveFile() { string Frompath = @"D:\Test\OutPut"; string directoryPath = @"D:\report"; try { string[] picList = Directory.GetFiles(Frompath, "*.jpg"); //图片 string[] txtList = Directory.GetFiles(Frompath, "*.txt"); //文本文件 string[] pdfList = Directory.GetFiles(Frompath, "*.pdf"); //PDF文件 foreach (string f in picList) { //取得文件名. string fName = f.Substring(Frompath.Length + 1); File.Copy(Path.Combine(Frompath, fName), Path.Combine(directoryPath, fName), true); } foreach (string f in txtList) { string fName = f.Substring(Frompath.Length + 1); try { File.Copy(Path.Combine(Frompath, fName), Path.Combine(directoryPath, fName)); } // 捕捉异常. catch (IOException copyError) { MessageBox.Show(copyError.Message); } } foreach (string f in pdfList) { string fName = f.Substring(Frompath.Length + 1); try { File.Copy(System.IO.Path.Combine(Frompath, fName), System.IO.Path.Combine(directoryPath, fName)); } catch (IOException copyError) { MessageBox.Show(copyError.Message); return; } } //删除原始文件夹里的文件 foreach (string f in txtList) { File.Delete(f); } foreach (string f in picList) { File.Delete(f); } foreach (string f in pdfList) { File.Delete(f); } } catch (DirectoryNotFoundException dirNotFound) { MessageBox.Show(dirNotFound.Message); } }
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
这篇文章主要介绍了C 实现拷贝文件到另一个文件夹下,具有很好的参考价
本周(6月19日-21日)A股市场急转直下,四川板块大幅跳水,重庆板块则
您购买的进口护肤品已完结清关,这是购物小票,请留意物流信息并及时收
方的文案1、愿得一人心,免得老相亲。2、既然选择了远方,我们便只顾风
端午天气预报预计端午节前期,全市以多云天气为主,局地有雷阵雨天气;
据媒体报道,近日有消费者反映,南航的国际航班在线值机选座“吃相难看
快科技6月22日消息,今天是端午节,大家都吃粽子了吗?由于我国地域跨
□周学泽“五月五,是端阳;吃粽子,挂香囊;门插艾,香满堂;龙舟下水
6月21日,盘江股份(600395)融资买入363 03万元,融资偿还400 84万元
6月21日20时40分许,宁夏银川市兴庆区一烧烤店发生燃气爆炸。经过紧急
国家税务总局、财政部6月21日发布公告,明确进一步优化研发费用加计扣
广东首家三甲职业病专科医院揭牌
【来源:金溪县气象台】金溪县气象台2023年06月21日06时29分发布雷电黄
hello大家好,我是城乡经济网小晟来为大家解答以上问题,九阴真经所有
6月21日电,三羊马公告,公司向不特定对象发行可转换公司债券申请获得