/// <summary> /// 删除文件夹下文件 /// </summary> /// <param name="paths"></param> public void DeleteFolder(string paths) { foreach (string d in Directory.GetFileSystemEntries(paths)) { if (File.Exists(d)) { FileInfo fi = new FileInfo(d); if (fi.Attributes.ToString().IndexOf("ReadOnly") != -1) fi.Attributes = FileAttributes.Normal; File.Delete(d);//直接删除其中的文件 } else { DirectoryInfo d1 = new DirectoryInfo(d); if (d1.GetFiles().Length != 0) { DeleteFolder(d1.FullName);////递归删除子文件夹 } Directory.Delete(d); } } } /// <summary> /// 删除文件夹及其内容 /// </summary> /// <param name="dir"></param> public void DeleteFolder1(string dir) { foreach (string d in Directory.GetFileSystemEntries(dir)) { if (File.Exists(d)) { FileInfo fi = new FileInfo(d); if (fi.Attributes.ToString().IndexOf("ReadOnly") != -1) fi.Attributes = FileAttributes.Normal; File.Delete(d);//直接删除其中的文件 } else DeleteFolder(d);////递归删除子文件夹 Directory.Delete(d); } }
/// <summary> /// 逐级 逐级创建文件夹并判断是否存在 /// </summary> /// <param name="url"></param> /// <returns></returns> private string CreateDir(string url) { string[] paths = url.Split('/'); string path = ""; for (int i = 0; i < paths.Length; i++) { path = Path.Combine(path, paths[i]); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } } return path; }