Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

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[^]
C#
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();
    }
}
 
Share this answer
 
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.. :)

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