★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公眾號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/ )
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10741596.html
➤如果鏈接不是山青詠芝的博客園地址,則可能是爬取作者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持作者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
代碼:
1 /// <summary> 2 /// 修改文件后綴 3 /// </summary> 4 /// <param name="extension">文件后綴</param> 5 private void UpdateExtension(string extension) 6 { 7 //彈框選擇文件夾 8 FolderBrowserDialog dialog = new FolderBrowserDialog 9 { 10 Description = "請選擇文件夾" 11 }; 12 if (dialog.ShowDialog() == DialogResult.OK) 13 { 14 //獲得文件夾路徑 15 string foldPath = dialog.SelectedPath; 16 if (!string.IsNullOrEmpty(foldPath)) 17 { 18 //初始化文件夾對象 19 DirectoryInfo dir = new DirectoryInfo(foldPath); 20 // 獲取當前文件夾下的所有文件 21 //TopDirectoryOnly:在搜索操作中包括僅當前目錄 22 FileInfo[] files = dir.GetFiles("*.*", SearchOption.TopDirectoryOnly); 23 //遍歷當前文件夾下的所有文件 24 for (int i = 0; i < files.Length; i++) 25 { 26 //獲取並輸出文件擴展名稱 27 Console.WriteLine(Path.GetExtension(files[i].FullName)); 28 //修改文件擴展名稱 29 files[i].MoveTo(Path.ChangeExtension(files[i].FullName, extension)); 30 //獲取並輸出文件擴展名稱 31 Console.WriteLine(Path.GetExtension(files[i].FullName)); 32 } 33 } 34 } 35 }
測試:
1 //注意不需要加'.' 2 UpdateExtension("swift");