473,406 Members | 2,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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?

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.IO;
  11. using System.Drawing.Printing;
  12.  
  13. namespace WindowsFormsApplication1
  14. {
  15.     public partial class Form1 : Form
  16.     {
  17.  
  18.  
  19.         public Form1()
  20.         {
  21.             InitializeComponent();
  22.         }
  23.  
  24.         private void button1_Click(object sender, EventArgs e)
  25.         {
  26.             FolderBrowserDialog FBD = new FolderBrowserDialog();
  27.  
  28.             if (FBD.ShowDialog() == DialogResult.OK)
  29.             {
  30.                 listBox1.Items.Clear();
  31.                 string[] files = Directory.GetFiles(FBD.SelectedPath);
  32.                 string[] dirs = Directory.GetDirectories(FBD.SelectedPath);
  33.  
  34.  
  35.  
  36.                 foreach (string file in files)
  37.                 {
  38.                     listBox1.Items.Add(Path.GetFileName(file));
  39.  
  40.  
  41.                 }
  42.                 foreach (string dir in dirs)
  43.                 {
  44.                     listBox1.Items.Add(Path.GetFileName(dir));
  45.                 }
  46.  
  47.             }
  48.  
  49.         }
  50.  
  51.         private StreamReader streamtoprint;
  52.         private void button2_Click(object sender, EventArgs e)
  53.         {
  54.  
  55.  
  56.             PrintDialog printDlg = new PrintDialog();
  57.             PrintDocument printDoc = new PrintDocument();
  58.             streamtoprint = new StreamReader("C:\\MyFiles.txt");
  59.  
  60.             printDoc.DocumentName = streamtoprint.ToString();
  61.             printDlg.Document = printDoc;
  62.             printDlg.AllowSelection = true;
  63.             printDlg.AllowSomePages = true;
  64.             //Call ShowDialog
  65.             if (printDlg.ShowDialog() == DialogResult.OK)
  66.                 printDoc.Print();
  67.  
  68.         }
  69.  
  70.     }
  71. }
May 8 '14 #1

✓ answered by Luk3r

Create a new project and play with the code I'm providing. I think it will give you a lot of answers.
Expand|Select|Wrap|Line Numbers
  1.         //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''''
  2.         //and formatted/modified it for C# and for teaching purposes
  3.         private void ChooseFileNameAndPrintButton_Click(object sender, EventArgs e)
  4.         {
  5.             if (openFileDialog1.ShowDialog() == DialogResult.OK)
  6.             {
  7.                 filename = openFileDialog1.FileName.ToString();
  8.                 ProcessStartInfo psi = new ProcessStartInfo();
  9.                 psi.UseShellExecute = true;
  10.                 psi.Verb = "print";
  11.                 psi.WindowStyle = ProcessWindowStyle.Hidden;
  12.                 psi.Arguments = printDialog1.PrinterSettings.PrinterName.ToString();
  13.                 psi.FileName = filename;
  14.                 Process.Start(psi);
  15.             }

14 3051
Luk3r
300 256MB
Create a new project and play with the code I'm providing. I think it will give you a lot of answers.
Expand|Select|Wrap|Line Numbers
  1.         //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''''
  2.         //and formatted/modified it for C# and for teaching purposes
  3.         private void ChooseFileNameAndPrintButton_Click(object sender, EventArgs e)
  4.         {
  5.             if (openFileDialog1.ShowDialog() == DialogResult.OK)
  6.             {
  7.                 filename = openFileDialog1.FileName.ToString();
  8.                 ProcessStartInfo psi = new ProcessStartInfo();
  9.                 psi.UseShellExecute = true;
  10.                 psi.Verb = "print";
  11.                 psi.WindowStyle = ProcessWindowStyle.Hidden;
  12.                 psi.Arguments = printDialog1.PrinterSettings.PrinterName.ToString();
  13.                 psi.FileName = filename;
  14.                 Process.Start(psi);
  15.             }
May 8 '14 #2
Thank you ...this works :) ..
May 8 '14 #3
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 ...
May 8 '14 #4
Luk3r
300 256MB
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.

Expand|Select|Wrap|Line Numbers
  1.         {
  2.  
  3.             if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
  4.             {
  5.                 string[] filenames = System.IO.Directory.GetFiles(folderBrowserDialog1.SelectedPath, "*");
  6.                 foreach (string testString in filenames)
  7.                 {
  8.  
  9.                     ProcessStartInfo psi = new ProcessStartInfo();
  10.                     psi.UseShellExecute = true;
  11.                     psi.Verb = "print";
  12.                     psi.WindowStyle = ProcessWindowStyle.Hidden;
  13.                     psi.Arguments = printDialog1.PrinterSettings.PrinterName.ToString();
  14.                     psi.FileName = testString;
  15.                     Process.Start(psi);
  16.                 }
  17.  
  18.             }
  19.         }
May 8 '14 #5
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.Directory.GetFiles(folderBrowserDialog1. SelectedPath, "*");' ... i can understand that you are printing the file paths stored in string array ...
May 8 '14 #6
Luk3r
300 256MB
Yeah. All that does is get the files (System.IO.Directory.Getfiles) from the path that you selected from the folder browser (folderBrowserDialog1.SelectedPath), 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.
May 8 '14 #7
Expand|Select|Wrap|Line Numbers
  1. private void Form1_Load(object sender, EventArgs e)
  2.         {
  3.             string Path = "C:\\Users\\AM6846\\Desktop\\Folder";
  4.             DirectoryInfo dir = new DirectoryInfo(Path);
  5.             this.listBox1.Items.AddRange(dir.GetDirectories());
  6.             textBox1.Text = Path;
  7.  
  8.  
  9.         }
  10.        // string filename;
  11.  
  12.         private void ChooseFileNameAndPrintButton_Click(object sender, EventArgs e)
  13.         {
  14.  
  15.             string Path1;
  16.  
  17.              string[] filenames = System.IO.Directory.GetFiles(listBox1., "*");
  18.  
  19.  
  20.  
  21.                  foreach (string testString in filenames)
  22.                  {
  23.  
  24.                      ProcessStartInfo psi = new ProcessStartInfo();
  25.                      psi.UseShellExecute = true;
  26.                      psi.Verb = "print";
  27.                      psi.WindowStyle = ProcessWindowStyle.Hidden;
  28.                      psi.Arguments = printDialog1.PrinterSettings.PrinterName.ToString();
  29.                      psi.FileName = testString;
  30.                      Process.Start(psi);
  31.                  }
  32.  
  33.              }
  34.  
  35.         }


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?
May 12 '14 #8
Luk3r
300 256MB
First things first:
1)
Expand|Select|Wrap|Line Numbers
  1. 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.SelectedItem.ToString() = "C:\\Users\\AM6846\\Desktop\\Folder" 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:
Expand|Select|Wrap|Line Numbers
  1. this.listBox1.Items.AddRange(""C:\\Users\\AM6846\\Desktop\\Folder"" & dir.Getdirectories());
May 12 '14 #9
I tried
Expand|Select|Wrap|Line Numbers
  1. 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 ...
May 12 '14 #10
Luk3r
300 256MB
In my #2 I gave you the property listbox with its property that you should use. listBox1.itemvalue does not exist nor do I know where you got that property information from.
May 12 '14 #11
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.selectedvalue...
May 12 '14 #12
Luk3r
300 256MB
As I said, use the SelectedItem property instead.

Replace:
Expand|Select|Wrap|Line Numbers
  1. string[] filenames = System.IO.Directory.GetFiles(listBox1., "*");
With:
Expand|Select|Wrap|Line Numbers
  1. string[] filenames = System.IO.Directory.GetFiles(listBox1.SelectedItem.ToString(), "*");
May 12 '14 #13
Thank you it is fine ...
May 12 '14 #14
Luk3r
300 256MB
As I also stated above: "2) Is the listBox1 item actually a directory? Example: does listBox1.SelectedItem.ToString() = "C:\\Users\\AM6846\\Desktop\\Folder" 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:
Expand|Select|Wrap|Line Numbers
  1.         private void Form1_Load(object sender, EventArgs e)
  2.         {
  3.             string Path = "C:\\Users\\AM6846\\Desktop\\Folder";
  4.             DirectoryInfo dir = new DirectoryInfo(Path);
  5.             textBox1.Text = Path;
  6.  
  7.             foreach (var fileOrFolder in dir.GetDirectories())
  8.             {
  9.                 listBox1.Items.Add(Path + @"\" + fileOrFolder);
  10.             }
  11.         }
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.
May 12 '14 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Michael A. Covington | last post by:
Has anyone implemented something like OpenFileDialog that *also* allows you to select folders? I know about the folder-browser tool, which *only* lets you see and select folders, not whole...
1
by: Michael Howes | last post by:
I'm working in C#/.Net Desktop shortcuts to folders are now shown in the FolderBrowserDialog. Is there any way to have them show up? thanks mike
0
by: Michael A. Covington | last post by:
Is there a simple way to modify OpenFileDialog (in either Win32 or .NET) so that the user can select folders as well as files? I know about FolderBrowser, which does not show any files at all and...
3
by: Rajiv Das | last post by:
VS 2003, XP SP2 ------------------------------------------------------------ DirectoryInfo temporary = new DirectoryInfo( Environment.GetFolderPath(Environment.SpecialFolder.InternetCache));...
5
by: Roy | last post by:
My dll file is copied to the "%windir%\Microsoft.NET\Framework\{version}Temporary ASP.NET Files" folder when I compile my asp.net project. When I run the application. The dll under this folder is...
9
by: Michael A. Covington | last post by:
Is there any way to hack OpenFileDialog (either in .NET 2.0 or in Win32) that will allow the user to select a folder rather than a file? Better yet, multiselect them. I know about...
1
by: SteveM | last post by:
I am writing a ToolUsage tracker which we will be accessing from each of our custom written tools. It will basically capture various pieces of information about the user, and the application they...
8
by: philqw78 | last post by:
I get sent a folder containing zip files. I extract the zips to another folder. This then gives me 65 sub folders(directories), each with an excel and a text file. I wish to open all the excel...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.