public static List <Item> GetItems(BackupRecord br, string path) { var items = new List <Item>(); foreach (var directory in br.GetDirectories(path)) { DirectoryItem item; if (path.Equals(@"\") || path.Length <= 0) //fix when path="\" { item = new DirectoryItem { ItemName = directory.ItemName, ItemPath = directory.ItemPath, ItemChecked = false, Items = GetItems(br, directory.ItemPath + directory.ItemName) }; } else { item = new DirectoryItem { ItemName = directory.ItemName, ItemPath = directory.ItemPath, ItemChecked = false, Items = GetItems(br, directory.ItemPath + @"\" + directory.ItemName) }; } items.Add(item); } foreach (FileItem file in br.GetFiles(path)) { FileItem item = new FileItem { ItemName = file.ItemName, ItemPath = file.ItemPath, ItemChecked = false, FileInfo = file.FileInfo }; items.Add(item); } return(items); }
/**/ //Console.WriteLine(); //Console.WriteLine("file checkati:"); foreach (Item i in checkedFileList) { myFileInfo info = ((FileItem)i).FileInfo; checkedFileInfoList.Add(((FileItem)i).FileInfo); //Console.WriteLine("\t" + ((FileItem)i).FileInfo.Name); } return(checkedFileInfoList); } /*get all checked files starting from a Directory*/ private static List <Item> getCheckedFilesRec(DirectoryItem directory) { List <Item> checkedFileList = new List <Item>(); foreach (Item i in directory.Items) { if (typeof(DirectoryItem) == i.GetType()) { checkedFileList.AddRange(getCheckedFilesRec((DirectoryItem)i)); } else if (i.ItemChecked) { checkedFileList.Add(i); } } return(checkedFileList); }
/*if path is null return directories in root, else return all subdirectories in "path" directory*/ public List <Item> GetDirectories(string path) { List <Item> directoryList = new List <Item>(); if (path == null || path.Length == 0) { path = "\\"; } //Console.WriteLine("#############"); //Console.WriteLine("Folders in \"" + path + "\" :"); foreach (myFileInfo fileInfo in fileInfoList) { /*find directories in "path" directory * - sub directory path must begin with "path/" * - sub directory path must be longer than "path" directory one */ string directoryPath = fileInfo.RelativePath; //(fix)relativePath in myFileInfo is null for files in the root folder if (directoryPath == null || directoryPath.Length <= 0) { directoryPath = @"\"; } string comparePath = path + @"\"; if (path.Equals(@"\")) { comparePath = @"\"; } if (directoryPath.StartsWith(comparePath) && directoryPath.Length > path.Length) { //find first occourence of "\", needed to find the name of the subfolder int endSubDirName = directoryPath.IndexOf(@"\", path.Length + 1); if (endSubDirName != -1) { endSubDirName -= 1; } else { endSubDirName = directoryPath.Length - 1; } //int end = directoryPath.Length - 1; string subDirName; if (path.Length <= 1) //fix when path="\" { subDirName = directoryPath.Substring(path.Length, endSubDirName - path.Length + 1); } else { subDirName = directoryPath.Substring(path.Length + 1, endSubDirName - path.Length); } DirectoryItem subDir = new DirectoryItem { ItemPath = directoryPath.Substring(0, path.Length), ItemName = subDirName, ItemChecked = false }; //Console.WriteLine("Name: "+ subDir.Name); //Console.WriteLine("Path: "+ subDir.Path); //Console.WriteLine(""); //add subfolders only once if (directoryList.Find(x => x.ItemName.Equals(subDir.ItemName) && x.ItemPath.Equals(subDir.ItemPath)) == null) { directoryList.Add(subDir); } } } //foreach (Item i in directoryList) // Console.WriteLine("\t-" + i.ItemName); return(directoryList); }