C#关于文件夹遍历以及文件拷贝
生活随笔
收集整理的這篇文章主要介紹了
C#关于文件夹遍历以及文件拷贝
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
C#關(guān)于文件夾遍歷以及文件拷貝
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 將一個(gè)文件夾內(nèi)的文件傳送到另一個(gè)文件夾內(nèi),包含子文件/// <summary>/// 將一個(gè)文件夾內(nèi)的文件傳送到另一個(gè)文件夾內(nèi),包含子文件/// </summary>/// <param name="source">源文件夾</param>/// <param name="destination">目標(biāo)文件夾</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);}}} }總結(jié)
以上是生活随笔為你收集整理的C#关于文件夹遍历以及文件拷贝的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于VMware虚拟机安装windows
- 下一篇: 一文搞定C#关于NPOI类库的使用读写E