Learning BackgroundWorker - Works in Debug Not After Compile
All the program does is goes to a directory, finds all excel files inside of the directory and returns the file names of each file to an array. After backgroundworker completes it puts the file names into a listbox... Works great in debug, not after building.
private string[] filename = null; public string[] fileNames private void searchButton_Click(object sender, EventArgs e) if (searchTextBox.Text == "") private void backgroundwork_Compeleted(object sender, RunWorkerCompletedEventArgs e) } private void searchDirectory_DoWork(object sender, DoWorkEventArgs e) Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application(); excelApp.Application.ScreenUpdating = false; excelApp.Application.FileSearch.NewSearch(); private void cancelButton_Click(System.Object sender, System.EventArgs e) Thanks in advance for your help!
{
get { return filename; }
set { filename = value; }
}
{
this.Cursor = Cursors.WaitCursor;
searchButton.Enabled = false;
cancelButton.Enabled = true;
{
MessageBox.Show("You must enter a string to perform a search.", "Search Failed");
this.Cursor = Cursors.Default;
searchButton.Enabled = true;
cancelButton.Enabled = false;
}
else
{
listBox1.Items.Clear();
backgroundWorker1.RunWorkerAsync();
}
this.Cursor = Cursors.Default;
}
{
searchButton.Enabled = true;
cancelButton.Enabled = false;
if (e.Error != null)
{
MessageBox.Show(e.Error.Message);
}
else if (e.Cancelled)
{
MessageBox.Show("The operation was cancelled by the user.", "Cancelled");
}
else
{
if (fileNames != null)
{
listBox1.Items.AddRange(fileNames);
}
}
{
string myDir = @"C:\ExcelFiles";
string entStr = "";
string _fnames = null;
int fileCount = 0;
char delim = ',';
entStr = searchTextBox.Text;
excelApp.Application.FileSearch.LookIn = myDir;
excelApp.Application.FileSearch.SearchSubFolders = false;
excelApp.Application.FileSearch.FileType = MsoFileType.msoFileTypeExcelWorkbooks;
if (excelApp.Application.FileSearch.Execute(MsoSortBy.msoSortByFileName, MsoSortOrder.msoSortOrderAscending, true) > 0)
{
foreach (string vaFileName in excelApp.Application.FileSearch.FoundFiles)
{
fileCount++;
if (_fnames == null)
{
_fnames = vaFileName;
}
else
{
_fnames = _fnames + "," + vaFileName;
}
}
}
if (_fnames != null)
{
fileNames = _fnames.Split(delim);
}
else
{
MessageBox.Show("There were no files in the folder.", "No Files");
}
}
{
this.backgroundWorker1.CancelAsync();
cancelButton.Enabled = false;
searchButton.Enabled = true;
}

