Note that there are some explanatory texts on larger screens.

plurals
  1. POBackground Worker to update multiple items in a listview
    text
    copied!<p>I am trying to update multiple values in a list box using this method: What this method does is copy the contents of the specified directory but it outputs the current file name to the background worker progress changed event. </p> <pre><code>private bool CopyDirectory(string SourcePath, string DestinationPath, bool Recursive, BackgroundWorker worker, DoWorkEventArgs e) { List&lt;string&gt; Paths = new List&lt;string&gt;(); List&lt;string&gt; Files = new List&lt;string&gt;(); //for windows xp My documents if(!Directory.Exists(SourcePath)) { SourcePath = SourcePath.Replace(@"C$\Users\", @"C$\Documents and Settings"); int doccount = Regex.Matches(SourcePath, "Documents").Count; //if source contains 2 documents if(doccount &gt; 1) { ReplaceLastOccurrence(SourcePath, "Documents", "My Documents"); } } //set file permissions FileIOPermission f2 = new FileIOPermission(FileIOPermissionAccess.Read, SourcePath); f2.AllLocalFiles = FileIOPermissionAccess.Read; f2.Demand(); foreach (string StringPath in DirectoryList(SourcePath, Recursive, null)) Paths.Add(StringPath); foreach (string StringPath in FileList(SourcePath, Recursive, null)) Files.Add(StringPath); try { foreach (string dirPath in Paths) System.IO.Directory.CreateDirectory(dirPath.Replace(SourcePath, DestinationPath)); foreach (string newPath in Files) { string[] filename = newPath.Split('$'); string currentfile = "C:" + filename[1]; //report the file name to the background worker worker.ReportProgress(0, currentfile); System.IO.File.Copy(newPath, newPath.Replace(SourcePath, DestinationPath), Recursive); } return true; } catch { return false; } } private static List&lt;string&gt; FileList(string RootDirectory, bool SearchAllDirectories, Predicate&lt;string&gt; Filter) { List&lt;string&gt; retList = new List&lt;string&gt;(); try { List&lt;string&gt; DirList = new List&lt;string&gt; { RootDirectory }; if (SearchAllDirectories) DirList.AddRange(DirectoryList(RootDirectory, SearchAllDirectories, Filter)); foreach (string DirectoryStr in DirList) { DirectoryInfo di = new DirectoryInfo(DirectoryStr); try { foreach (FileInfo FileStr in di.EnumerateFiles()) { try { if ((Filter == null) || (Filter(FileStr.FullName))) retList.Add(FileStr.FullName); } catch (Exception) { } } } catch (UnauthorizedAccessException) { } catch (Exception) { } } } catch (Exception) { } return retList; } private static List&lt;string&gt; DirectoryList(string RootDirectory, bool SearchAllDirectories, Predicate&lt;string&gt; Filter) { List&lt;string&gt; retList = new List&lt;string&gt;(); try { DirectoryInfo di = new DirectoryInfo(RootDirectory); foreach (DirectoryInfo DirectoryStr in di.EnumerateDirectories()) { try { if ((Filter == null) || (Filter(DirectoryStr.FullName))) { retList.Add(DirectoryStr.FullName); if (SearchAllDirectories) retList.AddRange(DirectoryList(DirectoryStr.FullName, SearchAllDirectories, Filter)); } } catch (UnauthorizedAccessException) { } catch (Exception) { } } } catch (Exception) { } return retList; } </code></pre> <p>This works great for just one file name update, however, my program will do multiple copying of directors that i wish to update a list-view item to display it to the user.</p> <pre><code>private void bgwBackup_ProgressChanged(object sender, ProgressChangedEventArgs e) { this.InvokeEx(c =&gt; this.lblBackupStatus.Text = e.UserState.ToString()); } </code></pre> <p>My question is how would I update multiple listviewitems with the current filename of the specified path ? I can post my do work handler if needed. Thank you.</p> <p>EDIT1 Here is my DO_WORK as requested: </p> <pre><code> private void bgwBackup_DoWork(object sender, DoWorkEventArgs e) { //Textbox Array Values // 0 = computername // 1 = username // 2 = password string[] tbvalues = (string[])e.Argument; string computer = tbvalues[0]; string user = tbvalues[1]; string pass = tbvalues[2]; try { //AD SEARCH DirectoryEntry de = new DirectoryEntry("WinNT://" + Environment.UserDomainName + "/" + user); string homepath = de.Properties["homedirectory"].Value.ToString(); if (string.IsNullOrWhiteSpace(homepath)) { //do something when home drive is missing MessageBox.Show("NO H DRIVE"); } else { //Declare strings string os = GetOsName(computer); string desktop=""; string documents=""; string favorites=""; string outlooksig = ""; string outlookcache = ""; string hdriveroot = homepath + @"\PKBACKUP " + DateTime.Now.ToString("MM-dd-yyyy--hh-mm-ss"); string hudp = hdriveroot + @"\Desktop"; string hudoc = hdriveroot + @"\Documents"; string hufav = hdriveroot + @"\Favorites"; string hprintdir = hdriveroot + @"\printers\"; string outlooksigdir = hdriveroot + @"\Outlook Files\Signatures"; string outlookcachedir = hdriveroot + @"\Outlook Files\Cache"; if (os.Contains("XP")) { desktop = @"\\" + computer + @"\C$\Documents and Settings\" + user + @"\Desktop"; documents = @"\\" + computer + @"\C$\Documents and Settings\" + user + @"\My Documents"; favorites = @"\\" + computer + @"\C$\Documents and Settings\" + user + @"\Favorites"; //outlook signatures outlooksig = @"\\" + computer + @"\C$\Documents and Settings\" + user + @"\Application Data\Microsoft\Signatures"; //outlook cache outlookcache = @"\\" + computer + @"\C$\Documents and Settings\" + user + @"\Application Data\Microsoft\Outlook"; } else { desktop = @"\\" + computer + @"\C$\Users\" + user + @"\Desktop"; documents = @"\\" + computer + @"\C$\Users\" + user + @"\Documents"; favorites = @"\\" + computer + @"\C$\Users\" + user + @"\Favorites"; outlooksig = @"\\" + computer + @"\C$\Users\" + user + @"\AppData\Roaming\Microsoft\Signatures"; outlookcache = @"\\" + computer + @"\C$\Users\" + user + @"\AppData\Roaming\Microsoft\Outlook"; } //Copy Files using (new Impersonator(user, "Domain.org", pass)) { string outlookext = "nk2"; CopyDirectory(favorites, hufav, true, bgwBackup, e); CopyDirectory(documents, hudoc, true, bgwBackup, e); CopyDirectory(desktop, hudp, true, bgwBackup, e); CopyDirectory(outlooksig, outlooksigdir, true, bgwBackup, e); copybyext(outlookcache, outlookcachedir, outlookext, user); //copy nk2 file method //Printers List&lt;string&gt; printers = new List&lt;string&gt;(); foreach (string printername in lbBackupprinters.Items) { if (printername.Contains("No Printers Found")) { //Add listview entry that no printers were found } else { if (Directory.Exists(hprintdir) == false) { Directory.CreateDirectory(hprintdir); } string prntsvr; string printer; //strip out numbers in string prntsvr = Regex.Replace(printername, "[^0-9]+", string.Empty); //take the first number in the string prntsvr = prntsvr[0].ToString(); //remote everything before last comma printer = printername.Substring(printername.LastIndexOf(',') + 1); //add vbs to printer name printer = printer + ".vbs"; //get file path of printer vbs script string[] printerpaths = Directory.GetFiles(@"\\dist-win-prnt-" + prntsvr + @"\printerscripts", printer); //append string builder with print server path if (!File.Exists(printerpaths[0])) { //do something if the printer does not exist, add error. } else { printers.Add(printerpaths[0]); } } string defaultprinter = lbBackupprinters.Items[0].ToString() + ".vbs"; defaultprinter = defaultprinter.Substring(defaultprinter.LastIndexOf(',') + 1); // //Download printer vbs script(s) foreach (string printstring in printers) { string hprintfilename = printstring.Substring(printstring.LastIndexOf("\\") + 1); if (hprintfilename.Equals(defaultprinter)) { File.Copy(printstring, hprintdir + "(Defualt) "+ hprintfilename, true); } else { File.Copy(printstring, hprintdir + hprintfilename, true); } } } // end else } } } //end try catch (Win32Exception logonfail) { MessageBox.Show("LOGON FAIL" + logonfail); //add label for failure return; } catch (DirectoryServicesCOMException adfail) { MessageBox.Show(adfail.ToString()); } catch (UnauthorizedAccessException accessex) { MessageBox.Show(accessex.ToString()); } } </code></pre> <p>EDIT2: Image of program <img src="https://i.stack.imgur.com/aYCFG.jpg" alt="enter image description here"></p> <p>EDIT3: Here is my progress changed and completed code </p> <pre><code> //this only updates the status text and does not update the listview control. private void bgwBackup_ProgressChanged(object sender, ProgressChangedEventArgs e) { this.InvokeEx(c =&gt; this.lblBackupStatus.Text = e.UserState.ToString()); } private void bgwBackup_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { lblBackupStatus.Text = "Backup job completed"; }` </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload