Click here to Skip to main content
15,880,503 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I currently have program that merges word. doc files together via user input. Once the user chooses an input folder and output destination, the documents are combined and the total number of files merged is displayed. here is an example of the code.
C#
MessageBox.Show("A total of " + sourceFiles.Count() + " documents have been merged", 
"Success", MessageBoxButtons.OK, MessageBoxIcon.Information);

The program and string.count work perfectly at the moment, however the issue I have is when I update the input folder's contents or choose to the output folder to be the same as in the input folder whilst the program is running

For Example: If I choose the input and output folders desired and run the program successfully, the user will eventually be greeted with a message box saying "a total of X documents have been merged" once the new file has been created.

If I drag an another file into the Input folder and immediately press the 'merge' button again, the "X documents merged" total will remain the same, even though an additional file has been inserted into that folder. The program will continue to make the combined files however the string.count total doesn't recognise the newly added documents. It's as if the program doesn't update folder's total contents

sourceFiles is declared as a
C#
private string [] 
and is called when the user selects their desired folder using a folder browser dialog.
C#
private void browseButton_Click(object sender, EventArgs e)
    {
        FolderBrowserDialog diagBrowser = new FolderBrowserDialog();
        diagBrowser.Description = "Select a folder which contains files needing combined...";

        // Default folder, altered when the user selects folder of choice 
        string selectedFolder = "";
        diagBrowser.SelectedPath = selectedFolder;

        // initial file path display
        folderPath.Text = diagBrowser.SelectedPath;

        // 'OK' button being confirmed on the popup menu
        if (DialogResult.OK == diagBrowser.ShowDialog())
        {
            // Grab the folder that was chosen
            selectedFolder = diagBrowser.SelectedPath;
            folderPath.Text = diagBrowser.SelectedPath;
            sourceFiles = Directory.GetFiles(selectedFolder, "*.doc"); 
  }


The merge command is then called, combining the sourceFiles (total number of documents in the folder), the output file name which includes a date / time and page break between the files
C#
MsWord.Merge(sourceFiles, outputFileName, pageBreaker);

Any suggestions to amend this issue? It is a small inconvenience however I would love to get it addressed

What I have tried:

Calling the sourceFiles when the user selects the merge button as opposed to when they select their input button
Posted
Updated 29-Mar-16 6:31am

1 solution

Your sourceFiles array will not be updated just because you added a new file to the selectedFolder. You need to get the sourceFiles array when you start the merge.
So move the
C#
sourceFiles = Directory.GetFiles(selectedFolder, "*.doc");
to be in the merge button handler.

You seem to indicate that you tried this. What happened? didn't happen?
 
Share this answer
 
Comments
cgraham720 29-Mar-16 14:12pm    
i had initially tried this however it was affecting my if / if-else statements. i have since got both aspects to work correctly after some frustrating attempts. thanks again for the advice. glad to see i was on the right track.
Sascha Lefèvre 29-Mar-16 16:09pm    
5ed
Matt T Heffron 29-Mar-16 16:17pm    
Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900