Click here to Skip to main content
14,336,321 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi guys please I need help, I am building a c# application all I want to do is load in bunch of file names

for instance re78905lts.pdf, le45608.pdf and goto a folder in c:\\slimmy.

Compare these items with those in slimyyyy, if an item ie re78905lts.pd exist in the folder send it to the default printer for printing,please I need help , I am totally going nuts .Presently I can browse and load these files and format them how I want but the next step is do the matching listBox items to folder(slimyyy)=>"c:\\slimyyyy" and send to print .
But first I want to be able to loop through items in listbox if any item in listbox exist in the said folder "c:\\slimyyyy" send it to the default printer for printing.
Thanks in advance for your suggestions .
Posted
Updated 20-Feb-15 2:13am
v2
Comments
ZurdoDev 20-Feb-15 8:13am
   
What's the problem? Just loop through the files and see if they exist in the listbox.
BillWoodruff 20-Feb-15 8:26am
   
Do one minute of research on "printing a file" here in CodeProject.
Ekona_Pikin 20-Feb-15 8:35am
   
Yes Ryan the goal is to look at items in in the listBox which could be RE4507S.pdf and if this item exist in "c:\\slimyyy" send it to the default printer.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

So what you need to do is compare the selected item from the listbox or all the items in the list box (up to you), to the GetFiles call.

If there is a match print the file.

Also Explained further here[^]
private void btnPrint_Click(object sender, EventArgs e) {
//FolderBrowserDialog fbg = new FolderbrowserDialog();

//if (fbd.ShowDialog() == DialogResult.Ok) {
    string dir = @"C:\slimeyyyy";
    if (Directory.Exists(dir)) {
        string[] files = Directory.GetFiles(dir);
        if (files.Length > 0) {

            // To print documents in any order based on the current file
            foreach (string file in files) {
                string fileName = Path.GetFileName(file);
                foreach (object item in listBox1.Items) {
                    if (fileName == item.ToString()) {
                        PrintDocument(Path.GetFullPath(file));
                    }
                }
            }

            // To print documents starting with the first item in the list box.
            for (int i = 0; i < listBox1.Items.Count; i++) {
                foreach (string file in files) {
                    string fileName = Path.GetFileName(file);
                    if (fileName == listBox1.Items[i].ToString()) {
                        PrintDocument(Path.GetFullPath(file));
                    }
                }
            }
        }
    }
}

private void PrintDocument(string path) {
    ProcessStartInfo info = new ProcessStartInfo();
    info.Verb = "print";
    info.FileName = path;
    info.CreateNoWindow = true;
    info.WindowStyle = ProcessWindowStyle.Hidden;

    Process p = new Process();
    p.StartInfo = info;
    p.Start();

    p.WaitForInputIdle();
    System.Threading.Thread.Sleep(3000);
    if (!p.CloseMainWindow()) {
        p.Kill();
    }
}
   
v4
Comments
Ekona_Pikin 20-Feb-15 18:07pm
   
Hi Ramza360,you are awesome, there is an issue I have implemented your code
but I adobe just try to open a nd closes back right off. I dont know why
and also I dont want to select an item each time before brows a folder .
I dont knoiw if there would be an easier way to just make a click_Event
whereby all items in the are scanned and /or compared with that of selected folder in this case c:\\slimyyyy ,an if an item in the listbox say re4506ly.pdf exist in slimyy ==> send to the default printer..for printing.
Thanks you very much.
Ramza360 20-Feb-15 18:26pm
   
Well the code above does check through all the items without you selecting one however this can be moved to a click event. Just substitute the dir with the slimeyy directory path. As for the adobe opening and closing that may depend on your settings. For instance on my comp I see no window it just prints.
Ramza360 20-Feb-15 18:47pm
   
I just updated the solution to use a 'click' event. The issue with adobe showing and closing is most likely environment specific. I am adding a link to another code project article that addresses this.
Ekona_Pikin 20-Feb-15 19:07pm
   
Awesome I am changing right now , you are a savior Ramza, thanks a bunch for all the help.
Ramza360 20-Feb-15 19:31pm
   
Hope it all works for you.. :)
Ekona_Pikin 21-Feb-15 10:21am
   
Hi Ramza, please sorry I dont know if am missing something
everything seems to work well but when I go to my printer there is no printed stuff..
Ramza360 21-Feb-15 10:37am
   
It may have to do with the sleep. I have noticed that 3 seconds may be to fast. However this may also force adobe to show up again..
Ekona_Pikin 21-Feb-15 10:38am
   
so can I change it to like maybe 10,0000?
Ramza360 21-Feb-15 10:40am
   
That would probably suffice. I'm looking to see if msdn has any other options as well, which seems there is one lengthy set of instructions for sending raw data to the printer, but that shouldn't be need in this case.
Ramza360 21-Feb-15 10:44am
   
also try adding this to ensure adobe is opened minimized.
info.Arguments = @"/p /h";
Ekona_Pikin 21-Feb-15 10:43am
   
ok
Ramza360 21-Feb-15 10:50am
   
also might be good to just make sure its entering the methods, try setting some breakpoints, check to see what the path string contains and whatnot.
Ramza360 21-Feb-15 11:19am
   
so its still just not printing? Seems a bit odd. And the adobe reader opening at all is a pain in the butt. Try that link i posted too, it goes into more detail on this subject, right here on CodeProject. I'm sorry I can't be of much more help in this regard.
Ekona_Pikin 21-Feb-15 12:31pm
   
ok thanks alot
Ekona_Pikin 21-Feb-15 13:15pm
   
Hey Ramza, I reduced the System.Threading.Thread.Sleep(3000);==>(1000)
and my printer sees it now:))
I will test it well Monday at work ,I really appreciate all your support.
Thanks again
Ramza360 21-Feb-15 13:16pm
   
Well that's awesome. Glad its working better. I figured lowering the time would cause other issues.. Hope the testing goes well :)

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