编写程序综合应用Directory类的主要方法。首先确定指定的目录是否存在,如果存在,则删除该目录;如果不存在,则创建该目录。然后,移动此目录,在其中创建一个文件,并对文件进行计数。 【解答】 using System; using System.IO; class Test { public static void Main() { string path = @"c:/MyDir"; string target = @"c:/TestDir"; try { if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } if (Directory.Exists(target)) { Directory.Delete(target, true); } Directory.Move(path, target); File.CreateText(target + @"/myfile.txt"); Console.WriteLine("在{0}中的文件数目是{1}", target, Directory.GetFiles(target).Length); } catch (Exception e) { Console.WriteLine("操作失败: {0}", e.ToString()); } finally { } } }