Click here to Skip to main content
14,326,728 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi guys I have this app that does printing based on items on a listbox.
by commatching those items with that in a directory :\slim\slimyyyy
I want to put an "ERROR CHECKING" that would give me a message that in item
on the listbox is not present in the directory .
For instance if there are 8 items or more is not in the said directory give message with the item thats not in the directory.
Find below is my code ,but my try catch does nothing.Any help is very welcome
Thanks in advance.
{
     //var printList = new List();
     try
     {
         var printList = new List();
         string dir = @"C:\slim\slimyyyy";
         if (Directory.Exists(dir))
         {
             string[] pdf_specFiles = Directory.GetFiles(dir);
             if (pdf_specFiles.Length > 0)
             {
                 foreach (object item in listBox1.Items)
                 {
                     foreach (string file in pdf_specFiles)
                     {
                         string fileName = Path.GetFileName(file);
                         if (fileName == item.ToString())
                         {
                             printList.Add(Path.GetFullPath(file));
                         }
                     }
                 }
                 foreach (string file in printList)
                 {
                     PrintDocument(file);
                     System.Threading.Thread.Sleep(10000); // wait 10 second say
                     Application.DoEvents(); // keep UI responsive if Windows Forms app
                 }
             }
         }
     }
     catch (Exception)
     {
         MessageBox.Show("You are missing Item(s).", "ERROR");
     }
 }>
Posted
Rate this:
Please Sign up or sign in to vote.

Solution 1

Hello again Ekona, This would be quite simple, well depending on how you want them to be notified.

----- inside method
if (pdf_specFiles.Length > 0) {
    // Only partial code from your solution above.
    bool itemFound = false;
    foreach (object item in listBox1.Items)
    {
        foreach (string file in pdf_specFiles)
        {
            string fileName = Path.GetFileName(file);
            if (fileName == item.ToString())
            {
                printList.Add(Path.GetFullPath(file));
                itemFound = true;
            }
        }
    
        // Send an message for every item unless you want to send one large out put message.
        if (!itemFound) {
            OutputErrorMessage(item.ToString());
        }
    }
}
---- outside method
    
void OutputErrorMessage(string message) {
    MessageBox.Show(message);
}


Keep in mind that would output an error message for each missing file.
This may not be so good, perhaps send one message for all missing files at the end.
You can use a string and keep += to it, though this is not an efficient way if there are many missing files. The alternative is using a StringBuilder.
----- Inside the method
if (pdf_specFiles.Length > 0) {
    // Only partial code from your solution above.
    StringBuilder builder = new StringBuilder();
    
    // The flag
    bool itemFound = false;
    
    foreach (object item in listBox1.Items)
    {
        foreach (string file in pdf_specFiles)
        {
            string fileName = Path.GetFileName(file);
            if (fileName == item.ToString())
            {
                printList.Add(Path.GetFullPath(file));
                itemFound = true;
            }
        }
        
        // Add the item to the list of missing items.
        if (!itemFound) {
            // Append to the builder
            if (builder.Length == 0) {
                builder.AppendLine("Missing Files");
            }
            builder.AppendLine(item.ToString());
        }
    }
    foreach (string file in printList)
    {
        PrintDocument(file);
        System.Threading.Thread.Sleep(10000); // wait 10 second say
        Application.DoEvents(); // keep UI responsive if Windows Forms app
    }
    
    if (builder.Length > 0) {
        OutputErrorMessage(builder.ToString());
    }
}

------- Outside the method

public void OutputErrorMessage(string messages) {
    MessageBox.Show(messages, "You are missing files");
}
   
v2
Comments
Ramza360 6-Mar-15 15:07pm
   
I'd suggest the second, as it will give them one message box with the list of missing files at the end while the printing is occurring. This way you wont block the other actions.
Ramza360 6-Mar-15 15:09pm
   
Using File.Exists(file) would work as suggested by OriginalGriff too, but you'd still have to decide which way to show the files in the message box.
Ekona_Pikin 6-Mar-15 15:09pm
   
WORKED LIKE CHARM ,man you are awesome thank you very much God bless you man.
Ramza360 6-Mar-15 15:10pm
   
No Problem :)
BillWoodruff 6-Mar-15 19:28pm
   
This is good, but, Ramza350, are you are doing the OP's coding FOR them rather than helping them learn ?
Ramza360 6-Mar-15 23:27pm
   
Yes this is a good point. I will keep it in mind in the future.
Rate this:
Please Sign up or sign in to vote.

Solution 2

Without seeing your PrintDocument code, we can't tell if your code will even throw an exception when a file doesn't exist - this could be why this does nothing...

Rather than relying on exceptions to tell you what is going on, I would prefer to explicitly check for the existence of the file in the code, and not issue the PrintDocument request in the first place.
foreach (string file in printList)
{
    if (File.Exists(file))
    {
        PrintDocument(file);
        System.Threading.Thread.Sleep(10000); // wait 10 second say
        Application.DoEvents(); // keep UI responsive if Windows Forms app
    }
    else
        ...
}

And why are you using Sleep and DoEvents in the loop anyway?
If you need to do DoEvents to maintain your UI, then you should have moved all this code onto a background thread to keep your UI clear - which means you don;t need DoEvents at all; and using Sleep on your UI thread for ten seconds is just going to mean that your UI thread stops and becomes unresponsive...
   
Comments
Ekona_Pikin 6-Mar-15 15:12pm
   
Hey thanks OriginalGriff,but I used the solution above it was just what I wanted
BillWoodruff 6-Mar-15 19:27pm
   
+5 ridiculous down-votes countered.

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100