c# - Open a directory and process files/folders in background worker query -
I have the following code, I am trying to open a directory and to process its files through the background worker I am trying to get problems with it.
I have an error (the current file does not exist in context), which I can understand because it is stored in any other method? If someone can tell me what is wrong in my code, then it will be appreciated. Folderbrowser does not work under background worker stream
Private Zero btnFiles_Click (Object Sender, EventArgs e) {//btnFiles.Enabled = false. BtnSTOP.Enabled = true; // Clear the text field list box Resesults.Items.Clear (); ListBoxPath.Items.Clear (); TxtItemsFound.Text = string Empty; // Open Folder Folder folder to select the user to open the folder DialogResult result = folder BrowserDialog1.ShowDialog (); If (Results == DialogResult.OK) {// Store selected folder path string filePath = folderBrowserDialog1.SelectedPath; } // start the async operation here. Ronwerker sink (); } Private Zero backgroundWorker1_DoWork (object sender, DoWorkEventArgs e) {// Try the process folder {foreach (string dir Alphaleonis.Win32.Filesystem.Directory.EnumerateFiles (in filepath, "*. *", SearchOption.AllDirectories, true)) {/ / Populate the list box with all the files it got. Invoke (new action (() => updateUpdate2 (dir))); FileInfo fi = New FileInfo (DIR); If (fi.Length == 0) {// Populated list box found this.Invoke with all empty files (new action ((=)) UpdateUpdate1 (dir + Environment.NewLine)); }}} // Hold hold of exception (exception mistake) {// This code is just written out of the message and continues to recurse log.Add (err.Message); //throw; } Finally {// add the count of blank files to txtItemsFound.Text = listBoxResults.Items.Count.ToString (); // Write all the files that can not be processed. Foreign (string in log) {this.Invoke (new action ((=); updateUpdate1 (S));); } Logs. clean (); MessageBox.Show ("full scanning", "end", message box button. OK, message box icon. Exclamation); } // If the cancellation button is pressed, then execution is in progress / cancellation --- change state ---> canceled (background worker cancels) (E. canil = true; // backgroundWorker1 .ReportProgress (0); return;} // // Report completed 100% completed after background 1. Report progress (100);}
< Div class = "post" itemprop = "text">
variable "FilePath" is being declared local to the btnFiles_Click method, in order to use it somewhere else , It should be declared global on code page:
public class form1 {private string} _filePath = null; private zeros btnFiles_Click (object sender, EventArgs e) {// get your file and here _filePath allocated ... _filePath = folderBrowserDialog1.SelectedPath;} Private Zero backgroundWorker1_DoWork (Object Sender, DoWorkEventArgs E) {// Usage _filePath here ...}}
Comments
Post a Comment