fsItem dirWalk(string sDir, int parentId, string tab) { /*if (sDir==@"C:\Users\Public\Pictures\felix\Picasa\Exports") { string s = sDir+""; }*/ fsItem folder = new fsItem(); folder.Path = sDir; folder.isFile = false; folder.ParentId = parentId; folderProcessArgs newFolderProcess; step++; // if (step > 1) { stop = true; } DirectoryInfo directory = new DirectoryInfo(sDir); FileInfo[] files = null; string[] folders; try { folders = Directory.GetDirectories(sDir); if (folders.Count() > 0) { //create a group newFolderProcess = new folderProcessArgs(this, folder, "has-children", "...Ignoring Items with no access, please check!"); OnNewFolder(this, newFolderProcess); } files = directory.GetFiles(); if ((folders.Count() + files.Count()) > 0) { List<fsItem> folderItems = new List<fsItem>(); string insertNewItemCommand = "INSERT INTO folders (parentId,path,isFile) values (" + parentId.ToString() + ",'" + processSQLTestParam(sDir) + "',0)"; DBCommand.CommandText = insertNewItemCommand; DBCommand.ExecuteNonQuery(); DBCommand.CommandText = @"select last_insert_rowid()"; folder.Id = (int)(long)DBCommand.ExecuteScalar(); // Need to type-cast since `ExecuteScalar` returns an object. //generate here an event and capture it and call report progress pn the background worker every times an event is captured newFolderProcess = new folderProcessArgs(this, folder, "started", "\r\n" + tab + "Processing Folder :'" + sDir + "'..."); OnNewFolder(this, newFolderProcess); if (stop) { folders = new string[0]; files = new FileInfo[0]; } //process folders foreach (string subfolder in folders) { fsItem fsi = dirWalk(subfolder, folder.Id, tab + "\t"); if (fsi != null) { if (fsi.Access > 0) { folder.Access = -1; } folderItems.Add(fsi); } } //process files processFilesInFolder(ref folderItems, folder, files); if (folderItems.Count > 0) { Dictionary<string, object> folderHash = getMD5FolderHash(folderItems); //at the end of the processing the hash and size are calculated and assigned folder.Hash = folderHash["hash"].ToString(); folder.Size = (long)folderHash["size"]; updateFolder(ref folder); newFolderProcess = new folderProcessArgs(this, folder,"completed", "\r\n" + tab + (this.stop ? "Exited" : "Done!")); OnNewFolder(this, newFolderProcess); return folder; } else { if (this.stop)// the system received a stop signal don't care the rest of the logic { newFolderProcess = new folderProcessArgs(this, folder,"stopping", "\r\n" + tab + "Exited"); OnNewFolder(this, newFolderProcess); } return null; } } else { //the folder is empty // calculate an empty folder hash Dictionary<string, object> folderHash = getMD5FolderHash(new List<fsItem>{folder}); //at the end of the processing the hash and size are calculated and assigned folder.Hash = folderHash["hash"].ToString(); folder.Size = (long)folderHash["size"]; updateFolder(ref folder); newFolderProcess = new folderProcessArgs(this, folder, "empty-complete", "\r\n" + tab + (this.stop ? "Exited" : "Done!")); OnNewFolder(this, newFolderProcess); return folder; } } catch (UnauthorizedAccessException ex) { newFolderProcess = new folderProcessArgs(this, folder,"ignored", "Access denied to folder :'" + sDir + "'" /*+ ex.Message*/); folder.Access = -1; OnNewFolder(this, newFolderProcess); return folder; //throw new Exception("\r\nError while getting files from:'" + sDir + "', with message:" + ex.Message); } catch (Exception ex) { throw new Exception("\r\nError while processing:'" + sDir + "', with message:" + ex.Message); } }
private void folderProc_OnNewFolder(object sender, folderProcessArgs e) { backgroundWorker1.ReportProgress(e.Folder.Id, e); }