C#关于文件夹遍历以及文件拷贝

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace FrmSearchXmlAndCopy
{public partial class Form1 : Form{public Form1(){InitializeComponent();}private void btnsearch_Click(object sender, EventArgs e){string timepath = DateTime.Now.ToString("yyyyMMdd");string strSNnumber = this.txtSN.Text.Trim();string parentPath = this.textPatent.Text.Trim();string outPath = this.textOut.Text.Trim();string filepath = parentPath + "\\" + timepath;DirectoryInfo folder = new DirectoryInfo(filepath);FileInfo[] fileInfos = folder.GetFiles();foreach (var item in fileInfos){// Console.WriteLine(item.Name);// string str = "B7#788#02.xml";//string[] strs = str.Split('#');string[] strs2 = item.Name.Split('#');if (strs2[0].Equals(strSNnumber)){Console.WriteLine(item.Name);Console.WriteLine(item.FullName);string targetPath = outPath + "\\" + "ITF\\XMLCEXPORT";this.label4.Text = "查找到此SN";this.label5.Text = "";// item.CopyTo(Path.Combine(targetPath,item.Name));MessageBox.Show("查找到此SN");break;}else {this.label5.Text = "查不找到此SN";this.label4.Text = "";}//  Console.WriteLine( strs[0]);// bool sd=strSNnumber.Equals(strs[0]);// Console.WriteLine(sd);}}#region  将一个文件夹内的文件传送到另一个文件夹内,包含子文件/// <summary>/// 将一个文件夹内的文件传送到另一个文件夹内,包含子文件/// </summary>/// <param name="source">源文件夹</param>/// <param name="destination">目标文件夹</param>private void CopyDiretory(string source, string destination){DirectoryInfo info = new DirectoryInfo(source);foreach (FileSystemInfo files in info.GetFileSystemInfos()){string FileName = files.Name;string BFileName = Path.Combine(destination, FileName);if (files is System.IO.FileInfo){if (!File.Exists(BFileName)){File.Copy(files.FullName, BFileName);}else{FileInfo fB = new FileInfo(BFileName);if (files.LastWriteTime > fB.LastWriteTime){if (MessageBox.Show(files.FullName + "文件已存在,是否覆盖?", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes){File.Delete(BFileName);File.Copy(files.FullName, BFileName);//覆盖,有问题}}}}else{Directory.CreateDirectory(BFileName);CopyDiretory(files.FullName, BFileName);}}}#endregionprivate void btncp_Click(object sender, EventArgs e){string timepath = DateTime.Now.ToString("yyyyMMdd");string strSNnumber = this.txtSN.Text.Trim();string parentPath = this.textPatent.Text.Trim();string outPath = this.textOut.Text.Trim();string filepath = parentPath + "\\" + timepath;DirectoryInfo folder = new DirectoryInfo(filepath);FileInfo[] fileInfos = folder.GetFiles();foreach (var item in fileInfos){// Console.WriteLine(item.Name);// string str = "B77#88021#00.xml";//string[] strs = str.Split('#');string[] strs2 = item.Name.Split('#');if (strs2[0].Equals(strSNnumber)){Console.WriteLine(item.Name);Console.WriteLine(item.FullName);string targetPath = outPath + "\\" + "ITF\\XMLCEXPORT";this.label4.Text = "查找到此SN";this.label5.Text = "";item.CopyTo(Path.Combine(targetPath, item.Name));// MessageBox.Show("查找到此SN");break;}else{this.label5.Text = "查不找到此SN";this.label4.Text = "";}//  Console.WriteLine( strs[0]);// bool sd=strSNnumber.Equals(strs[0]);// Console.WriteLine(sd);}}}
}

C#关于文件夹遍历以及文件拷贝相关推荐

  1. HALCON教程-初步:文件夹遍历,文件筛选,文件名拆分,图片读取及保存

    [1]文件夹遍历 list_image_files ( : : ImageDirectory, Extensions, Options : ImageFiles) ImageDirectory: 文件 ...

  2. QT编写实现图片的幻灯片播放、自适应显示、缩放(以鼠标位置为中心进行缩放)、拖动、重置、显示鼠标位置像素坐标及RGB值、播放GIF动画、截图保存、批量保存、拖入文件夹遍历所有文件

    这个图片查看器功能很多,是我花了不少心思,不断优化,不断添加功能的成果: 1.能打开并显示所有常用图片格式文件,显示鼠标位置像素坐标及RGB值 2.能缩放,拖动图片,可以以鼠标为中心滚动滚轮进行缩放 ...

  3. Python递归文件夹遍历所有文件夹及文件

    第一种 : #!/usr/bin/env python # --*-- coding:UTF-8 --*--import osdef file_name(file_dir):for home, dir ...

  4. c语言mkdir创建文件夹失败,cenos mkdir 无法创建文件夹,即便文件权限为777

    Java--如何创建文件夹及文件,删除文件,文件夹 package com.zz; import java.io.File; import java.io.IOException; /** * Jav ...

  5. 拷贝文件(包括文件夹中的文件夹)

    ''' 1.查看来源文件夹中的文件 2.通过遍历得到文件夹中的文件(文件名+扩展名) 3.拼接文件 (文件夹+文件名+扩展名) 4.判断来源文件中是 文件夹 还是 文件明.扩展名如果是文件夹则:.提前 ...

  6. OpenCV实现遍历文件夹下所有文件

    OpenCV中有实现遍历文件夹下所有文件的类Directory,它里面包括3个成员函数:(1).GetListFiles:遍历指定文件夹下的所有文件,不包括指定文件夹内的文件夹:(2).GetList ...

  7. python根据文件名或后缀名遍历文件夹下所有文件或图片的路径,并计算文件行数

    python遍历文件夹下所有文件或图片的路径 代码 指定目录下的指定后缀文件 import glob #获取指定目录下的所有图片 print (glob.glob("\\home\\qiao ...

  8. 怎么遍历服务器文件夹,遍历远程服务器某个文件夹下的文件

    遍历远程文件夹下的文件,您可以考虑使用FTP的方式,具体的请参考下述代码: private string GetFileNames(string URI) { WebClient wClient = ...

  9. python批量删除文件1001python批量删除文件_Python实现递归遍历文件夹并删除文件...

    思路: 遍历文件夹下面的文件夹 如果文件夹名称等于".svn",则修改文件夹的属性(因为".svn"的文件都是只读的,你不能直接删除) 删除此文件夹 如果文件夹 ...

最新文章

  1. 二叉树的序列化与反序列化
  2. Spring和JSF集成:导航
  3. 数据科学入门与实战:Matplotlib绘图基础一
  4. @SuppressWarnings(resource)
  5. java 数组不重复_java如何找出数组中的不重复数字
  6. python list 的深浅拷贝探索
  7. win11系统通知怎么取消 Windows11取消系统通知的步骤方法
  8. PASS云计算书简介——接近完美的模式
  9. win10环境下安装AutoCAD2006+南方CASS7.1 【家庭中文版和非家庭中文版】
  10. (long)123是c语言常量吗,C语言常量与变量
  11. ZYNQ BRAM_ctrl PS PL通信报错
  12. /etc.login.defs 权限权限解析
  13. opencv:图像的读取和显示
  14. 迪文屏学习系列之数据录入
  15. IDA 逆向代码 --- _stack_chk_guard变量 之后的局部 怎么处理
  16. python实现图片压缩_python如何实现图片压缩
  17. python中pd series_Python 数据处理(三)——pd.Series
  18. UCOS-Ⅲ:软件定时器
  19. broadccastreceiver
  20. vue3中使用canvas

热门文章

  1. Xilinx FPGA PLL 资源与INTEL FPGA PLL资源locked信号的不同
  2. mysql中if在oracle怎么用_MySql和Oracle的使用
  3. python字符串大全_python学习笔记:字符串操作大全
  4. python3seek_Python3 file.seek()方法
  5. 别小看不起眼的电阻,里面大有学问!
  6. 微信竟然可以查出行轨迹了,预计又一波情侣要分手?
  7. Linux!为何他一人就写出这么强的系统?
  8. 来看看,他的嵌入式之旅!
  9. 嗨,你选择自学成才还是参加培训?
  10. 大学学计算机的有作业吗,有没有电脑高手,大学计算机专业的作业