搜索文件和目录,然后复制文件
本文最后更新于 542 天前,其中的信息可能已经有所发展或是发生改变
来源吾爱破解 

界面很丑,没有使用 visual studio 写着实费劲!
%title插图%num 

加载文件功能: 把所有需要搜索的文件或者目录名 放到一个文本里面(一个放一行)。选中这个文件即可
前三个按键依次选中,开始就可以了!

编译程序时使用的控制台模式,所以有一个控制窗口。
给大家提供一个编译成窗口模式的方法(没有控制台窗口)。
需要进去控制台(cmd),然后更改目录至存放源代码目录(pushd d:\123\源代码目录)
1: 窗口模式
[Bash shell] 纯文本查看 复制代码csc /t:winexe 代码.cs
2:控制台模式
[Bash shell] 纯文本查看 复制代码csc /t:exe 代码.cs

百度云盘
https://pan.baidu.com/s/1cDJVnDUoPAPZqSg19AMnYg
提取码:hrj6

以下是源代码:
[C#] 纯文本查看 复制代码// csc FindsrcForm.cs using System; using System.IO; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; using System.Collections.Generic; using System.Threading; // 程序界面 public partial class HomeForm : Form { [STAThread] static void Main() { Application.EnableVisualStyles(); Application.Run(new HomeForm()); } } public partial class HomeForm { private string SelectedPath; private string SourcesrcName; private Label tipslbl; private TextBox SourceCententtxt; private string CopyToDirectory; private Label ShowSourcePathlbl; private Label ShowTargetPathlbl; private Label ShowCopyToDirPathlbl; public HomeForm() { // 文件按键 Button loadsrcBtn = new Button(); loadsrcBtn.Size = new Size(150, 30); loadsrcBtn.Location = new Point(30, 20); loadsrcBtn.Text = “加载源文件”; this.Controls.Add(loadsrcBtn); loadsrcBtn.Click += new EventHandler(loadsrcBtnClick); // 目录按键 Button selectFolderBtn = new Button(); selectFolderBtn.Size = new Size(150, 30); selectFolderBtn.Location = new Point(30, 80); selectFolderBtn.Text = “选择搜索目录”; this.Controls.Add(selectFolderBtn); selectFolderBtn.Click += new EventHandler(selectFolderBtnClick); // 复制文件存放目录按键 Button CopysrcDirBtn = new Button(); CopysrcDirBtn.Size = new Size(150, 30); CopysrcDirBtn.Location = new Point(30, 140); CopysrcDirBtn.Text = “选择复制文件存放目录”; this.Controls.Add(CopysrcDirBtn); CopysrcDirBtn.Click += new EventHandler(CopysrcDirBtnClick); // 开始任务按键 Button StartTaskBtn = new Button(); StartTaskBtn.Size = new Size(150, 30); StartTaskBtn.Location = new Point(30, 200); StartTaskBtn.Text = “开始”; this.Controls.Add(StartTaskBtn); StartTaskBtn.Click += new EventHandler(StartTaskBtnClick); ShowSourcePathlbl = new Label(); ShowSourcePathlbl.Font = new Font(ShowSourcePathlbl.Font.Name, 11); ShowSourcePathlbl.Size = new Size(500, 30); ShowSourcePathlbl.Location = new Point(200, 20); ShowSourcePathlbl.TextAlign = ContentAlignment.MiddleLeft; ShowSourcePathlbl.Text = “请选择源文件”; ShowTargetPathlbl = new Label(); ShowTargetPathlbl.Font = new Font(ShowSourcePathlbl.Font.Name, 11); ShowTargetPathlbl.Size = new Size(500, 30); ShowTargetPathlbl.Location = new Point(200, 80); ShowTargetPathlbl.TextAlign = ContentAlignment.MiddleLeft; ShowTargetPathlbl.Text = “请选择搜索目录”; ShowCopyToDirPathlbl = new Label(); ShowCopyToDirPathlbl.Font = new Font(ShowSourcePathlbl.Font.Name, 11); ShowCopyToDirPathlbl.Size = new Size(500, 30); ShowCopyToDirPathlbl.Location = new Point(200, 140); ShowCopyToDirPathlbl.TextAlign = ContentAlignment.MiddleLeft; ShowCopyToDirPathlbl.Text = “请选择复制文件存放目录”; this.Controls.Add(ShowCopyToDirPathlbl); this.Controls.Add(ShowTargetPathlbl); this.Controls.Add(ShowSourcePathlbl); // 显示信息 tipslbl = new Label(); tipslbl.Size = new Size(1000, 30); tipslbl.Location = new Point(30, 260); this.Controls.Add(tipslbl); // 源文件内容展示 SourceCententtxt = new TextBox() { WordWrap = true, Multiline = true, ScrollBars = ScrollBars.Both, }; SourceCententtxt.Size = new Size(680, 260); SourceCententtxt.Location = new Point(30, 300); this.Controls.Add(SourceCententtxt); this.Size = new Size(800, 600); } private void loadsrcBtnClick(object sender, EventArgs e) { SourcesrcName = UserSelectedsrc(Directory.GetCurrentDirectory()); tipslbl.Text = SourcesrcName; ShowSourcePathlbl.Text = SourcesrcName; SourceCententtxt.Lines = src.ReadAllLines(SourcesrcName, System.Text.Encoding.Default); } private void selectFolderBtnClick(object sender, EventArgs e) { SelectedPath = UserSelectedPath(Directory.GetCurrentDirectory()); tipslbl.Text = SelectedPath; ShowTargetPathlbl.Text = SelectedPath; } private void CopysrcDirBtnClick(object sender, EventArgs e) { CopyToDirectory = UserSelectedPath(Directory.GetCurrentDirectory()); tipslbl.Text = CopyToDirectory; ShowCopyToDirPathlbl.Text = CopyToDirectory; } private void StartTaskBtnClick(object sender, EventArgs e) { if (SourcesrcName == null || SelectedPath == null || CopyToDirectory == null) { return; } FindDirectoryAndsrcName fdf = new FindDirectoryAndsrcName(SourcesrcName, SelectedPath, CopyToDirectory); Thread th = new Thread(ThreadTask); th.Start(fdf); } // 线程任务 private void ThreadTask(object FDF) { FindDirectoryAndsrcName fdf; fdf = FDF as FindDirectoryAndsrcName; tipslbl.Text = “任务进行中!”; fdf.GetResult(); tipslbl.Text = “任务完成!”; } // 选择目标目录 public string UserSelectedPath(string defaultPath) { string res; FolderBrowserDialog fb = new FolderBrowserDialog(); fb.RootFolder = Environment.SpecialFolder.Desktop; fb.Description = “请选择待扫描目标目录”; if (defaultPath != null) { fb.SelectedPath = defaultPath; } while (true) { if (fb.ShowDialog() == DialogResult.OK) { res = fb.SelectedPath; break; } } return res; } // 选择文件 public string UserSelectedsrc(string defaultPath) { string srcFullName; var opensrcDialog = new OpensrcDialog() { Filter = “txt (*.txt)|*.txt|All (*.*)|*.*”, }; if (defaultPath != null) { opensrcDialog.InitialDirectory = defaultPath; } while (true) { if (opensrcDialog.ShowDialog() == DialogResult.OK) { srcFullName = opensrcDialog.srcName; break; } } return srcFullName; } } // 文件查找操作 public class FindDirectoryAndsrcName { private string Sourcesrc { get; set; } private string TargetPath { get; set; } private string NewDirectory { get; set; } private string[] SourcesrcContent { get; set; } private List<string> TargetAllDir; private List<string> TargetAllDirName { get; set; } private List<string> TargetAllsrc; private List<string> TargetAllsrcName { get; set; } public FindDirectoryAndsrcName(string sourcesrc, string targetPath, string newDirectory) { Sourcesrc = sourcesrc; TargetPath = targetPath; NewDirectory = newDirectory; SourcesrcContent = src.ReadAllLines(Sourcesrc, System.Text.Encoding.Default); TargetAllDirName = GetsrcName(Directory.EnumerateDirectories(TargetPath, “*”, SearchOption.AllDirectories), out TargetAllDir); TargetAllsrcName = GetsrcName(Directory.Enumeratesrcs(TargetPath, “*”, SearchOption.AllDirectories), out TargetAllsrc); } public void GetResult() { List<int> res; bool found = false; List<string> NotFound = new List<string>(); foreach (string source in SourcesrcContent) { found = false; // 复制目录(树) res = FindDirAndsrcName(source, TargetAllDirName); if (res.Count > 0) { found = true; foreach (int index in res) { // 复制目录结构 string newTargetDir = PathReplace(TargetAllDir[index], NewDirectory); if (!Directory.Exists(newTargetDir)) { Directory.CreateDirectory(newTargetDir); } foreach (string subDir in Directory.EnumerateDirectories(TargetAllDir[index], “*”, SearchOption.AllDirectories)) { string newSubDir = PathReplace(subDir, NewDirectory); if (!Directory.Exists(newSubDir)) { Directory.CreateDirectory(newSubDir); } } // 复制文件 foreach (string subsrc in Directory.Enumeratesrcs(TargetAllDir[index], “*”, SearchOption.AllDirectories)) { src.Copy(subsrc, PathReplace(subsrc, NewDirectory), true); } } } // Console.WriteLine(1); // 复制文件 res = FindDirAndsrcName(source, TargetAllsrcName); if (res.Count > 0) { found = true; foreach (int index in res) { string newsrcFullName = PathReplace(TargetAllsrc[index], NewDirectory); string newsrcFullDir = Path.GetDirectoryName(newsrcFullName); if (!Directory.Exists(newsrcFullDir)) { Directory.CreateDirectory(newsrcFullDir); } // Console.WriteLine(TargetAllsrc[index]); // Console.WriteLine(newsrcFullName); src.Copy(TargetAllsrc[index], newsrcFullName, true); } } if (!found) { // Console.WriteLine(“{0} 未找到!”, source); NotFound.Add(source); } } string NotFoundFullName = Path.Combine(Path.GetDirectoryName(Sourcesrc), “未找到.txt”); src.WriteAllLines(NotFoundFullName, NotFound, System.Text.Encoding.Default); } // 扫描目标目录文件目录结构 static List<string> GetsrcName(IEnumerable<string> Data, out List<string> ToTList) { List<string> srcName = new List<string>(); ToTList = new List<string>(); foreach (var dir in Data) { srcName.Add(Path.GetsrcName(dir)); ToTList.Add(dir); } return srcName; } // 查找文件或者目录 static List<int> FindDirAndsrcName(string source, List<string> Target) { List<int> result = new List<int>(); int index = Target.IndexOf(source); if (index == -1) return result; result.Add(index); while (true) { index = Target.IndexOf(source, index + 1); if (index != -1) { result.Add(index); } else { break; } } return result; } // 路径替换 static string PathReplace(string source, string newDirectory) { string newPath; // 判断是否在一个盘符下面 if (Path.GetPathRoot(source).ToUpper() == Path.GetPathRoot(newDirectory).ToUpper()) { int index = 0; while (index < newDirectory.Length) { if (char.ToUpper(source[index]) == char.ToUpper(newDirectory[index])) { index++; } else { break; } } while (true) { if (source[–index] == ‘\\’) { newPath = Path.Combine(newDirectory, source.Substring(index + 1)); break; } } } else { newPath = Path.Combine(newDirectory, source.Substring(3)); } return newPath; }}
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇