我命由己◆ licaihua11111
关注数: 8 粉丝数: 41 发帖数: 836 关注贴吧数: 20
文件夹下多余路径删除 public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btndetele_Click(object sender, EventArgs e) { try { MoveFolder(txtway.Text.ToString()); MessageBox.Show("该路径下多余路径已删除"); } catch(Exception ex) { MessageBox.Show(ex.Message); } } public static void MoveFolder(string Path) { string newpath = null; string newway = null; string deleteway = null; string[] paths = GetFilePathList(Path).ToArray(); foreach (string path in paths) { string[] deletepath = path.Split('\\'); for (int i = 0; i < deletepath.Length; i++) { if (i == 0) { newpath = deletepath[i]; newway = deletepath[i]; deleteway = deletepath[i]; } else { if (i != deletepath.Length - 2) { newpath = newpath + "\\" + deletepath[i]; } if ((i != deletepath.Length - 2) && (i != deletepath.Length - 1)) { newway = newway + "\\" + deletepath[i]; } if (i != deletepath.Length - 1) { deleteway = deleteway + "\\" + deletepath[i]; } } } int countway = GetFilesCount(newway); if (countway == 1) { File.Move(path, newpath); } } if (Directory.Exists(deleteway)) { Directory.Delete(deleteway, true); } } private static List<string> GetFilePathList(string rootPath) { List<string> flieList = new List<string>(); List<string> dirList = new List<string>(); dirList.Add(rootPath); for (int i = 0; i < dirList.Count; i++) { if (Directory.Exists(dirList[i])) { dirList.AddRange(Directory.GetDirectories(dirList[i])); flieList.AddRange(Directory.GetFiles(dirList[i])); } } return flieList; } public static int GetFilesCount(string newway) { int totalCount = Directory.GetDirectories(newway).Length; return totalCount; } }
1 下一页