I need to print files in folder from desktop...and the user can select folders..I am

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Aditya23
    New Member
    • May 2014
    • 11

    I need to print files in folder from desktop...and the user can select folders..I am

    Hi, I am new to programming and I'm trying to learn from this internship...I tried using folder browse dialog box option in tool box and used the print dialog box too to print the file...I can view all files in the folder but could not print all the files displayed...I have pasted the windows form code below ...can anyone help me please?

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.IO;
    using System.Drawing.Printing;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
    
           
            public Form1()
            {
                InitializeComponent();
            }
             
            private void button1_Click(object sender, EventArgs e)
            {
                FolderBrowserDialog FBD = new FolderBrowserDialog();
    
                if (FBD.ShowDialog() == DialogResult.OK)
                {
                    listBox1.Items.Clear();
                    string[] files = Directory.GetFiles(FBD.SelectedPath);
                    string[] dirs = Directory.GetDirectories(FBD.SelectedPath);
    
    
    
                    foreach (string file in files)
                    {
                        listBox1.Items.Add(Path.GetFileName(file));
                       
    
                    }
                    foreach (string dir in dirs)
                    {
                        listBox1.Items.Add(Path.GetFileName(dir));
                    }
    
                }
    
            }
    
            private StreamReader streamtoprint;
            private void button2_Click(object sender, EventArgs e)
            {
                
               
                PrintDialog printDlg = new PrintDialog();
                PrintDocument printDoc = new PrintDocument();
                streamtoprint = new StreamReader("C:\\MyFiles.txt");
              
                printDoc.DocumentName = streamtoprint.ToString();
                printDlg.Document = printDoc;
                printDlg.AllowSelection = true;
                printDlg.AllowSomePages = true;
                //Call ShowDialog
                if (printDlg.ShowDialog() == DialogResult.OK)
                    printDoc.Print();
                
            }
           
        }
    }
    Last edited by Rabbit; May 8 '14, 03:27 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.
  • Luk3r
    Contributor
    • Jan 2014
    • 300

    #2
    Create a new project and play with the code I'm providing. I think it will give you a lot of answers.
    Code:
            //Got this code from: http://social.msdn.microsoft.com/Forums/vstudio/en-US/5ed39f6c-76e1-4031-9c96-202612ecbf65/printing-documents-doc-xls-pdf-jpeg-etc-to-a-specific-printer?forum=vbgeneral''''
            //and formatted/modified it for C# and for teaching purposes
            private void ChooseFileNameAndPrintButton_Click(object sender, EventArgs e)
            {
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    filename = openFileDialog1.FileName.ToString();
                    ProcessStartInfo psi = new ProcessStartInfo();
                    psi.UseShellExecute = true;
                    psi.Verb = "print";
                    psi.WindowStyle = ProcessWindowStyle.Hidden;
                    psi.Arguments = printDialog1.PrinterSettings.PrinterName.ToString();
                    psi.FileName = filename;
                    Process.Start(psi);
                }

    Comment

    • Aditya23
      New Member
      • May 2014
      • 11

      #3
      Thank you ...this works :) ..

      Comment

      • Aditya23
        New Member
        • May 2014
        • 11

        #4
        Can you please tell me how to print files from a folder by just selecting the folder? ... by the above method i can print seperate files ...

        Comment

        • Luk3r
          Contributor
          • Jan 2014
          • 300

          #5
          I figured you could work from what I provided. Hopefully the following code helps you understand what needs to be done. Please feel free to ask any and all questions for an explanation of what's going on.

          Code:
                  {
                      
                      if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
                      {
                          string[] filenames = System.IO.Directory.GetFiles(folderBrowserDialog1.SelectedPath, "*");
                          foreach (string testString in filenames)
                          {
                              
                              ProcessStartInfo psi = new ProcessStartInfo();
                              psi.UseShellExecute = true;
                              psi.Verb = "print";
                              psi.WindowStyle = ProcessWindowStyle.Hidden;
                              psi.Arguments = printDialog1.PrinterSettings.PrinterName.ToString();
                              psi.FileName = testString;
                              Process.Start(psi);
                          }
          
                      }
                  }

          Comment

          • Aditya23
            New Member
            • May 2014
            • 11

            #6
            ya it works ..thank you.. I initially tried to use the folder dialog box and its value to be passed to the printer but i couldn't .... can you explain this line 'string[] filenames = System.IO.Direc tory.GetFiles(f olderBrowserDia log1.SelectedPa th, "*");' ... i can understand that you are printing the file paths stored in string array ...

            Comment

            • Luk3r
              Contributor
              • Jan 2014
              • 300

              #7
              Yeah. All that does is get the files (System.IO.Dire ctory.Getfiles) from the path that you selected from the folder browser (folderBrowserD ialog1.Selected Path), gets all files ("*"), and adds them to the string array. Then, in the foreach loop, it goes through the string array and actually grabs each file name which is then passed to the print dialog.

              Comment

              • Aditya23
                New Member
                • May 2014
                • 11

                #8
                Code:
                private void Form1_Load(object sender, EventArgs e)
                        {
                            string Path = "C:\\Users\\AM6846\\Desktop\\Folder";
                            DirectoryInfo dir = new DirectoryInfo(Path);
                            this.listBox1.Items.AddRange(dir.GetDirectories());
                            textBox1.Text = Path;
                          
                            
                        }
                       // string filename;
                        
                        private void ChooseFileNameAndPrintButton_Click(object sender, EventArgs e)
                        {
                
                            string Path1;
                
                             string[] filenames = System.IO.Directory.GetFiles(listBox1., "*");
                
                                              
                
                                 foreach (string testString in filenames)
                                 {
                
                                     ProcessStartInfo psi = new ProcessStartInfo();
                                     psi.UseShellExecute = true;
                                     psi.Verb = "print";
                                     psi.WindowStyle = ProcessWindowStyle.Hidden;
                                     psi.Arguments = printDialog1.PrinterSettings.PrinterName.ToString();
                                     psi.FileName = testString;
                                     Process.Start(psi);
                                 }
                
                             }
                        
                        }


                I can't pass the path address of selected list item into the print for each loop...can anyone give me some idea or a solution please?
                Last edited by Rabbit; May 12 '14, 05:03 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data. Second warning.

                Comment

                • Luk3r
                  Contributor
                  • Jan 2014
                  • 300

                  #9
                  First things first:
                  1)
                  Code:
                  string[] filenames = System.IO.Directory.GetFiles(listBox1., "*");
                  This line only says listBox1., but has no property chosen.

                  2) Is the listBox1 item actually a directory? Example: does listBox1.Select edItem.ToString () = "C:\\Users\\AM6 846\\Desktop\\F older" or does it = just the folder/item name?

                  3) If #2 is the latter, you would need to compose a name for each item added. Example:
                  Code:
                  this.listBox1.Items.AddRange(""C:\\Users\\AM6846\\Desktop\\Folder"" & dir.Getdirectories());

                  Comment

                  • Aditya23
                    New Member
                    • May 2014
                    • 11

                    #10
                    I tried
                    Code:
                    string[] filenames = System.IO.Directory.GetFiles(listBox1.itemvalue.selectedpath, "*");
                    but it won't run and displayed error.. No listbox1 contains folders name and when user selects a folder the folder path is sent to the print (foreach) to print all files within the selected folder.. My problem is that i couldnot pass the address of selected folder into the foreach(print) module ...

                    Comment

                    • Luk3r
                      Contributor
                      • Jan 2014
                      • 300

                      #11
                      In my #2 I gave you the property listbox with its property that you should use. listBox1.itemva lue does not exist nor do I know where you got that property information from.

                      Comment

                      • Aditya23
                        New Member
                        • May 2014
                        • 11

                        #12
                        No the thing is that ... I should not use folderbrowse dialog box...i should use the listbox to select the folder and print the files in it...i'm sorry it is listBox1.select edvalue...

                        Comment

                        • Luk3r
                          Contributor
                          • Jan 2014
                          • 300

                          #13
                          As I said, use the SelectedItem property instead.

                          Replace:
                          Code:
                          string[] filenames = System.IO.Directory.GetFiles(listBox1., "*");
                          With:
                          Code:
                          string[] filenames = System.IO.Directory.GetFiles(listBox1.SelectedItem.ToString(), "*");

                          Comment

                          • Aditya23
                            New Member
                            • May 2014
                            • 11

                            #14
                            Thank you it is fine ...
                            Last edited by Aditya23; May 12 '14, 06:34 PM. Reason: ok

                            Comment

                            • Luk3r
                              Contributor
                              • Jan 2014
                              • 300

                              #15
                              As I also stated above: "2) Is the listBox1 item actually a directory? Example: does listBox1.Select edItem.ToString () = "C:\\Users\\AM6 846\\Desktop\\F older" or does it = just the folder/item name?"

                              Each item in the directory you provided is being added as ONLY the file/folder name. It needs formatted in such a way that it will add the path string in front of the file name. Example:
                              Code:
                                      private void Form1_Load(object sender, EventArgs e)
                                      {
                                          string Path = "C:\\Users\\AM6846\\Desktop\\Folder";
                                          DirectoryInfo dir = new DirectoryInfo(Path);
                                          textBox1.Text = Path;
                              
                                          foreach (var fileOrFolder in dir.GetDirectories())
                                          {
                                              listBox1.Items.Add(Path + @"\" + fileOrFolder);
                                          }
                                      }
                              The reason we change .AddRange to .Add is because AddRange is not used for Strings. For this same reason, we use foreach to loop through each directory and simply concatenate the path string + a backslash + the name of the file or folder.

                              Comment

                              Working...