C#学习笔记(25)——用刻盘器批量从U盘删除添加文件

说明(2017-11-17 14:46:05):

1. 因为经常要从U盘里面删除版本,然后添加版本,每次都要几个人手动复制粘贴,费时费力,就花了一下午时间写了个程序,自动删除和添加版本。

2. DriverInfo类可以识别插到电脑的U盘,还能识别U盘容量。

3. 现在是先全部删除选中版本,再一个U盘一个U盘的往里拷贝,不知道用多线程是否能同时拷贝。

4. 如果遇到有一个U盘不能读取,软件会报错,提示未检测到路径,可以加个判断,判断一下U盘路径是否存在。不过我懒得改了,用之前先用刻盘软件检查一下有没有坏盘就好了。

5. 如果遇到里面已经存在这个版本了,目前是直接覆盖,不然会报错提示文件已存在,这里也可以判断一下是否存在这个文件,我也懒得改了。

6. 用c#在Windows系统操作文件还是很方便的,也不用找这个库那个库,我猜想如果用python做,估计找库就得找死,而且也没什么界面。

软件界面:

Form1.cs

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Windows.Forms;
  9 using System.IO;
 10 using System.Text.RegularExpressions;
 11 
 12 namespace ChangeBin
 13 {
 14     public partial class Form1 : Form
 15     {
 16         public Form1()
 17         {
 18             InitializeComponent();
 19         }
 20         //主程序
 21         private void Form1_Load(object sender, EventArgs e)
 22         {
 23             ShowSource();
 24             List<string> UdiskNames = GetUdiskNames();
 25             //foreach (string udiskName in UdiskNames)
 26             //{
 27             //    Console.WriteLine(udiskName);
 28             //}
 29 
 30         }
 31 
 32         private string[] GetInsertFileName()
 33         {
 34             List<string> fileNames = new List<string>();
 35             string[] chineseFile = Directory.GetFiles("../../res/1_语文");
 36             string[] mathFile = Directory.GetFiles("../../res/2_数学");
 37             string[] englishFile = Directory.GetFiles("../../res/3_英语");
 38 
 39 
 40             foreach (string name in chineseFile)
 41             {
 42                 fileNames.Add(name);
 43             }
 44             foreach (string name in mathFile)
 45             {
 46                 fileNames.Add(name);
 47             }
 48             foreach (string name in englishFile)
 49             {
 50                 fileNames.Add(name);
 51             }
 52             return fileNames.ToArray();
 53 
 54         }
 55         //全选右侧
 56         private void cb7_CheckedChanged(object sender, EventArgs e)
 57         {
 58             if (cb7.Checked)
 59             {
 60                 for (int i = 0; i < clb4.Items.Count; i++)
 61                 {
 62                     clb4.SetItemChecked(i, true);
 63                 }
 64                 for (int i = 0; i < clb5.Items.Count; i++)
 65                 {
 66                     clb5.SetItemChecked(i, true);
 67                 }
 68                 for (int i = 0; i < clb6.Items.Count; i++)
 69                 {
 70                     clb6.SetItemChecked(i, true);
 71                 }
 72             }
 73             else
 74             {
 75                 for (int i = 0; i < clb4.Items.Count; i++)
 76                 {
 77                     clb4.SetItemChecked(i, false);
 78                 }
 79                 for (int i = 0; i < clb5.Items.Count; i++)
 80                 {
 81                     clb5.SetItemChecked(i, false);
 82                 }
 83                 for (int i = 0; i < clb6.Items.Count; i++)
 84                 {
 85                     clb6.SetItemChecked(i, false);
 86                 }
 87             }
 88 
 89 
 90         }
 91 
 92 
 93         //清除左侧
 94         private void btnClear1_Click(object sender, EventArgs e)
 95         {
 96             for (int i = 0; i < clb1.Items.Count; i++)
 97             {
 98                 clb1.SetItemChecked(i, false);
 99             }
100             for (int i = 0; i < clb2.Items.Count; i++)
101             {
102                 clb2.SetItemChecked(i, false);
103             }
104             for (int i = 0; i < clb3.Items.Count; i++)
105             {
106                 clb3.SetItemChecked(i, false);
107             }
108         }
109 
110         //显示进度
111         private void cbShowBack_CheckedChanged(object sender, EventArgs e)
112         {
113             BackGround bg = new BackGround();
114             bg.Show();
115         }
116 
117         //获取U盘里的需要删除的文件路径(怎么获取U盘容量??????名字 ??)
118         private List<string> GetUdiskNames()
119         {
120             List<string> diskNames = new List<string>();
121             DriveInfo[] driveInfo = DriveInfo.GetDrives();
122             //15711846400byte
123             foreach (DriveInfo d in driveInfo)
124             {
125                 if (d.DriveType == DriveType.Removable)
126                 {
127                     diskNames.Add(d.Name);
128                 }
129             }
130             return diskNames;
131         }
132 
133         //显示主页面
134         private void ShowSource()
135         {
136             //获取需要拷贝的bin包名字和路径
137             string[] chineseFile = Directory.GetFiles("../../res/1_语文");
138             string[] mathFile = Directory.GetFiles("../../res/2_数学");
139             string[] englishFile = Directory.GetFiles("../../res/3_英语");
140 
141             List<string> chineseFileName = new List<string>();
142             List<string> mathFileName = new List<string>();
143             List<string> englishFileName = new List<string>();
144             foreach (string name in chineseFile)
145             {
146                 chineseFileName.Add(Regex.Split(name, @"1_语文\")[1]);
147             }
148             foreach (string name in mathFile)
149             {
150                 mathFileName.Add(Regex.Split(name, @"2_数学\")[1]);
151             }
152             foreach (string name in englishFile)
153             {
154                 englishFileName.Add(Regex.Split(name, @"3_英语\")[1]);
155             }
156             //把需要拷贝的文件名加入选择列表
157             clb4.Items.AddRange(chineseFileName.ToArray());
158             clb5.Items.AddRange(mathFileName.ToArray());
159             clb6.Items.AddRange(englishFileName.ToArray());
160         }
161 
162         //获取删除列表
163         private List<string> GetDeleteList()
164         {
165             List<string> UdiskNames = GetUdiskNames();
166             List<string> delNames = new List<string>();
167             List<string> delPaths = new List<string>();
168             foreach (var item in clb1.CheckedItems)
169             {
170                 delNames.Add(item.ToString());
171             }
172             foreach (var item in clb2.CheckedItems)
173             {
174                 delNames.Add(item.ToString());
175             }
176             foreach (var item in clb3.CheckedItems)
177             {
178                 delNames.Add(item.ToString());
179             }
180             foreach (string udiskName in UdiskNames)
181             {
182                 foreach (string delName in delNames)
183                 {
184                     if (delName.Contains("语文"))
185                     {
186                         delPaths.Add(udiskName + @"RootData
es1_语文" + delName + @".bin");
187                     }
188                     else if (delName.Contains("数学"))
189                     {
190                         delPaths.Add(udiskName + @"RootData
es2_数学" + delName + @".bin");
191                     }
192                     else if (delName.Contains("英语"))
193                     {
194                         delPaths.Add(udiskName + @"RootData
es3_英语" + delName + @".bin");
195                     }
196 
197                 }
198             }
199             return delPaths;
200         }
201         //获取插入列表
202         private List<string> GetInsertList()
203         {
204             List<string> InsertList = new List<string>();
205             //获取需要拷贝的bin包名字和路径
206             string[] chineseFile = Directory.GetFiles("../../res/1_语文");
207             string[] mathFile = Directory.GetFiles("../../res/2_数学");
208             string[] englishFile = Directory.GetFiles("../../res/3_英语");
209             foreach (string file in chineseFile)
210             {
211                 InsertList.Add(file);
212             }
213             foreach (string file in mathFile)
214             {
215                 InsertList.Add(file);
216             }
217             foreach (string file in englishFile)
218             {
219                 InsertList.Add(file);
220             }
221             foreach (string item in InsertList)
222             {
223                 Console.WriteLine(item);
224             }
225             return InsertList;
226         }
227 
228         //点击开始,开始删除,插入(要不要分开进行?????)
229         private void btnStart_Click(object sender, EventArgs e)
230         {
231             //删除
232             List<string> UdiskNames = GetUdiskNames();
233             List<string> delPaths = GetDeleteList();
234             if (delPaths.Count > 0)
235             {
236                 foreach (string delPath in delPaths)
237                 {
238                     //Console.WriteLine(delPath);
239 
240                     if (File.Exists(delPath))
241                     {
242                         File.Delete(delPath);
243                         Console.WriteLine("已删除" + delPath);
244                     }
245                     else
246                     {
247                         //为啥删完还会显示不存在?循环了两次?还是delPath里面存了两遍?
248                         Console.WriteLine(delPath + "不存在");
249                     }
250 
251                 }
252             }
253             else
254             {
255                 MessageBox.Show("没有要删除的bin包");
256             }
257             //插入
258             //获取需要拷贝的bin包名字和路径
259             List<string> insertList = GetInsertList();
260             List<string> chineseFileName = new List<string>();
261             List<string> mathFileName = new List<string>();
262             List<string> englishFileName = new List<string>();
263 
264             if (insertList.Count > 0)
265             {
266                 foreach (string uDiskName in UdiskNames)
267                 {
268                     foreach (string insertFile in insertList)
269                     {
270                         if (File.Exists(insertFile))
271                         {
272                             string desFileName = null;
273                             string insertFileName = null;
274                             if (insertFile.Contains("语文"))
275                             {
276                                 insertFileName = Regex.Split(insertFile, @"1_语文\")[1];
277                                 desFileName = uDiskName + @"RootData
es1_语文" + insertFileName;
278                             }
279                             else if (insertFile.Contains("数学"))
280                             {
281                                 insertFileName = Regex.Split(insertFile, @"2_数学\")[1];
282                                 desFileName = uDiskName + @"RootData
es2_数学" + insertFileName;
283                             }
284                             else if (insertFile.Contains("英语"))
285                             {
286                                 insertFileName = Regex.Split(insertFile, @"3_英语\")[1];
287                                 desFileName = uDiskName + @"RootData
es3_英语" + insertFileName;
288                             }
289                             if (desFileName != null)
290                             {
291                                 //第三个参数是否允许覆盖???
292                                 File.Copy(insertFile, desFileName,true);
293                                 Console.WriteLine("已插入:" + desFileName);
294                             }
295                         }
296                         else
297                         {
298                             Console.WriteLine("插入文件:" + insertFile + "不存在");
299                         }
300                     }
301                 }
302 
303             }
304             MessageBox.Show("拷贝完毕!");
305         }
306     }
307 }
原文地址:https://www.cnblogs.com/Jacklovely/p/7851861.html