两天内琢磨出的东西,主要技术:使用委托跨线程访问,文件/路径的一些操作,实现功能:改写某一文件夹内的所有文件名
效果图:
源代码:
1 using System; 2 using System.Drawing; 3 using System.Windows.Forms; 4 using System.Threading; 5 using System.IO; 6 7 namespace ReSetFileNameByList 8 { 9 public partial class Form1 : Form 10 { 11 public Form1() 12 { 13 InitializeComponent(); 14 } 15 16 Thread threadShowMessageToUser = null; 17 public delegate void ShowMessageToUser(); 18 public ShowMessageToUser myDelegate; 19 private void Form1_Load(object sender, EventArgs e) 20 { 21 rabtn_ReSetNameByRule.Checked = true;//默认选择按重命名规则命名 22 pan_reNameRule.Visible = true;//显示重命名区域 23 cob_FirstName.SelectedIndex = 0; 24 cob_SecendName.SelectedIndex = 0; 25 cob_ThirdName.SelectedIndex = 0; 26 27 myDelegate = new ShowMessageToUser(SetMsgShowToUser); 28 ShowMessageOnLable("", MessageType.None); 29 pan_reNameRule.Update(); 30 31 } 32 33 //打开选择文件路径对话框 34 private void btn_OpenFileDialog_Click(object sender, EventArgs e) 35 { 36 //选择文件夹对话框 37 //SaveFileDialog allFiles = new SaveFileDialog(); 38 //allFiles.Title = "选择文件夹-为其中所有文件重命名"; 39 //DialogResult result = allFiles.ShowDialog(); 40 41 FolderBrowserDialog allFiles = new FolderBrowserDialog(); 42 allFiles.ShowNewFolderButton = false;//不可以有“新建文件夹”按钮 43 allFiles.Description = "选择文件夹-将为其中的所有文件重命名"; 44 DialogResult result = allFiles.ShowDialog(); 45 46 if (result == DialogResult.Yes || result == DialogResult.OK) 47 { 48 txt_FilePathSource.Text = allFiles.SelectedPath; 49 allFiles.Dispose(); 50 } 51 ShowMessageOnLable("", MessageType.None); 52 53 } 54 55 //开始执行 56 private void btn_BeginReSetName_Click(object sender, EventArgs e) 57 { 58 #region 空值判断 59 if (txt_FilePathSource.Text.Length <= 0) 60 { 61 ShowMessageOnLable("请先填写目标文件夹!", MessageType.Error); 62 //MessageBox.Show("请先填写源文件夹!","小博提示",MessageBoxButtons.OK,MessageBoxIcon.Exclamation); 63 return; 64 } 65 if (rabtn_ReSetNameByRule.Checked == true) 66 { 67 if (cob_FirstName.Text.Length <= 0 && cob_SecendName.Text.Length <= 0 && cob_ThirdName.Text.Length <= 0) 68 { 69 ShowMessageOnLable("请填写重命名规则!", MessageType.Error); 70 //MessageBox.Show("请填写重命名规则!", "小博提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); 71 return; 72 } 73 } 74 if (rabtn_DelWords.Checked == true) 75 { 76 if (txt_delByWord.Text.Length <= 0) 77 { 78 ShowMessageOnLable("请填写文件名中包含的要删除的词!", MessageType.Error); 79 //MessageBox.Show("请填写文件名中包含的要删除的词!", "小博提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); 80 return; 81 } 82 } 83 #endregion 84 ShowMessageOnLable("正在执行....", MessageType.Ready); 85 //lal_msg.Invoke(myDelegate); 86 threadShowMessageToUser = new Thread(ThreadFunction); 87 threadShowMessageToUser.IsBackground = true;//设置为后台线程 88 threadShowMessageToUser.Start(); 89 } 90 //线程的方法 91 private void ThreadFunction() 92 { 93 if (InvokeRequired)//判断是否调用控件方法时是否必须调用Invoke方法 94 { 95 //lal_msg.Invoke(new ShowMessageToUser(ThreadFunction)); 96 lal_msg.Invoke(myDelegate); 97 } 98 else 99 { 100 SetMsgShowToUser(); 101 } 102 } 103 //委托的方法 104 public void SetMsgShowToUser() 105 { 106 string filePath = txt_FilePathSource.Text.Trim(); 107 ReSetFileName(filePath); 108 } 109 /// <summary> 110 /// 重命名文件 111 /// </summary> 112 /// <param name="filePath">文件的文件夹路径</param> 113 public void ReSetFileName(string filePath) 114 { 115 if (!Directory.Exists(filePath)) 116 { 117 ShowMessageOnLable("文件夹路径不正确!", MessageType.Error); 118 return; 119 } 120 if ((cob_FirstName.Text.Length <= 0 && cob_ThirdName.Text.Length <= 0) || (cob_FirstName.Text == "(不使用前缀)" && cob_ThirdName.Text == "(不使用后缀)")) 121 { 122 ShowMessageOnLable("前缀或后缀必须有一个!", MessageType.Error); 123 return; 124 } 125 //else if (!cob_FirstName.Text.Contains("一") && cob_ThirdName.Text.Contains("一")) 126 //{ 127 // ShowMessageOnLable("前缀或后缀的格式不正确必须得含有\"一\"!", MessageType.Error); 128 // return; 129 //} 130 try 131 { 132 string[] files = Directory.GetFiles(filePath); 133 for (int i = 0; i < files.Length; i++) 134 { 135 string ext = Path.GetExtension(files[i]); 136 137 string filePathNoExtension = Path.GetFileNameWithoutExtension(files[i]); 138 //MessageBox.Show(filePathNoExtension); 139 string newFileName = GetFileNewName(filePathNoExtension, i + 1); 140 141 string resultFileName = Path.Combine(filePath, newFileName + ext); 142 if (!String.Equals(files[i], resultFileName)) 143 { 144 Directory.Move(files[i], resultFileName); 145 } 146 int precent = Convert.ToInt32((((i + 1) * 1.00) / (files.Length * 1.00)) * 100); 147 ShowMessageOnLable(precent.ToString() + "% " + resultFileName, MessageType.Ready); 148 lal_msg.Update(); 149 Thread.Sleep(1000); 150 } 151 } 152 catch (Exception ex) 153 { 154 ShowMessageOnLable(ex.Message, MessageType.Exception); 155 } 156 } 157 /// <summary> 158 /// 对文件名进行逻辑处理并返回新文件名 159 /// </summary> 160 /// <param name="oldFileNameWithoutExt">旧文件名,不包含路径和扩展名</param> 161 /// <param name="index">循环计算次数</param> 162 /// <returns>返回值:新的文件名,不包含路径和扩展名</returns> 163 public string GetFileNewName(string oldFileNameWithoutExt, int index) 164 { 165 string fileNameByUser = ""; 166 #region 使用加前后缀的重命名规则 167 if (rabtn_ReSetNameByRule.Checked) 168 { 169 //数字123 0 170 //第一个 1 171 //第一项 2 172 //第一讲 3 173 //第一天 4 174 //(不使用前缀) 5 175 #region 前缀名字逻辑 176 switch (cob_FirstName.Text) 177 { 178 case "数字123": 179 if (cob_SecendName.Text.Length >= 0) 180 { 181 fileNameByUser += index + "_"; 182 } 183 else 184 { 185 fileNameByUser += index; 186 } 187 break; 188 case "第一个": 189 case "第一项": 190 case "第一讲": 191 case "第一天": 192 if (cob_SecendName.Text.Length >= 0) 193 { 194 fileNameByUser += cob_FirstName.Text.Replace("一", index.ToString()) + "_"; 195 } 196 else 197 { 198 fileNameByUser += cob_FirstName.Text.Replace("一", index.ToString()); 199 } 200 break; 201 case "(不使用前缀)": 202 break; 203 default: 204 if (cob_FirstName.Text.Length >= 0 && cob_FirstName.Text.Contains("一")) 205 { 206 if (cob_SecendName.Text.Length >= 0) 207 { 208 fileNameByUser += cob_FirstName.Text.Replace("一", index.ToString()) + "_"; 209 } 210 else 211 { 212 fileNameByUser += cob_FirstName.Text.Replace("一", index.ToString()); 213 } 214 } 215 break; 216 } 217 #endregion 218 #region 中间名字逻辑 219 if (cob_SecendName.Text == "原名") 220 { 221 fileNameByUser += oldFileNameWithoutExt; 222 } 223 if (cob_SecendName.Text.Length >= 0 && cob_SecendName.Text != "原名") 224 { 225 fileNameByUser += cob_SecendName.Text; 226 } 227 #endregion 228 #region 后缀名字逻辑 229 switch (cob_ThirdName.Text) 230 { 231 case "数字123": 232 if (cob_SecendName.Text.Length >= 0) 233 { 234 fileNameByUser += "_" + index; 235 } 236 else 237 { 238 fileNameByUser += index; 239 } 240 break; 241 case "第一个": 242 case "第一项": 243 case "第一讲": 244 case "第一天": 245 if (cob_SecendName.Text.Length >= 0) 246 { 247 fileNameByUser += "_" + cob_ThirdName.Text.Replace("一", index.ToString()); 248 } 249 else 250 { 251 fileNameByUser += cob_ThirdName.Text.Replace("一", index.ToString()); 252 } 253 break; 254 case "(不使用前缀)": 255 break; 256 default: 257 if (cob_ThirdName.Text.Length >= 0 && cob_ThirdName.Text.Contains("一")) 258 { 259 if (cob_SecendName.Text.Length >= 0) 260 { 261 fileNameByUser += cob_ThirdName.Text.Replace("一", index.ToString()) + "_"; 262 } 263 else 264 { 265 fileNameByUser += cob_ThirdName.Text.Replace("一", index.ToString()); 266 } 267 } 268 break; 269 } 270 #endregion 271 } 272 #endregion 273 if (rabtn_DelWords.Checked) 274 { 275 if (txt_delByWord.Text.Length >= 0) 276 { 277 fileNameByUser = oldFileNameWithoutExt.Replace(txt_delByWord.Text, ""); 278 } 279 } 280 return fileNameByUser; 281 } 282 //单选:选择了“删除包含词”的规则的事件处理 283 private void rabtn_DelWord_CheckedChanged(object sender, EventArgs e) 284 { 285 pan_reNameRule.Visible = false; 286 pan_delWords.Visible = true; 287 ShowMessageOnLable("", MessageType.None); 288 } 289 //单选:选择了“加前后缀重命名”的规则的事件处理 290 private void rabtn_ReSetNameByRule_CheckedChanged(object sender, EventArgs e) 291 { 292 pan_delWords.Visible = false; 293 pan_reNameRule.Visible = true; 294 295 ShowMessageOnLable("", MessageType.None); 296 } 297 enum MessageType 298 { 299 Exception, 300 Error, 301 Ready, 302 Succeed, 303 None 304 } 305 /// <summary> 306 /// 在窗体上显示不同颜色的消息 307 /// </summary> 308 /// <param name="msg">要显示的消息内容</param> 309 /// <param name="msgType">消息的类型,Exception:异常,紫红色显示,Error:错误,红色显示,None:无变化,不显示,Ready:已准备好,蓝色显示,Succeed:重命名成功,绿色显示</param> 310 private void ShowMessageOnLable(string msg, MessageType msgType) 311 { 312 if (msgType == MessageType.Exception) 313 { 314 lal_msg.ForeColor = Color.Fuchsia; 315 lal_msg.Text = msg; 316 } 317 else if (msgType == MessageType.Error) 318 { 319 lal_msg.ForeColor = Color.Red; 320 lal_msg.Text = msg; 321 } 322 else if (msgType == MessageType.None) 323 { 324 lal_msg.ForeColor = Color.Red; 325 lal_msg.Text = msg; 326 } 327 else if (msgType == MessageType.Succeed) 328 { 329 lal_msg.ForeColor = Color.Green; 330 lal_msg.Text = msg; 331 } 332 else if (msgType == MessageType.Ready) 333 { 334 lal_msg.ForeColor = Color.Blue; 335 lal_msg.Text = msg; 336 } 337 else 338 { 339 btn_BeginReSetName.Enabled = false; 340 lal_msg.ForeColor = Color.Red; 341 lal_msg.Text = "请重新运行程序!"; 342 } 343 } 344 private void txt_FilePathSource_TextChanged(object sender, EventArgs e) 345 { 346 ShowMessageOnLable("", MessageType.None); 347 } 348 } 349 }