[csharp] static void Main(string[] args) { Console.WriteLine("本程序去掉當前目錄及子目錄下的文件前綴"); Console.Write("請輸入要去掉的前綴:"); string stringFront = Console.ReadLine(); if (stringFront != "") { string dir = AppDomain.CurrentDomain.BaseDirectory; RenameFile(dir, stringFront); } else Console.WriteLine("請輸入要去掉的前綴!"); Console.WriteLine("操作已完成"); Console.ReadKey(); } public static void RenameFile(string ParentDir,string stringFront) { string[] files = Directory.GetFiles(ParentDir, "*.cs", SearchOption.TopDirectoryOnly); foreach (string file in files) { string filename = Path.GetFileName(file); string pathname = Path.GetDirectoryName(file); if (filename.StartsWith(stringFront, true, null)) { filename = filename.Substring(stringFront.Length); FileInfo fi = new FileInfo(file); fi.MoveTo(Path.Combine(pathname,filename)); } } string[] dirs = Directory.GetDirectories(ParentDir); foreach(string dir in dirs) { RenameFile(dir,stringFront); } }