469,963 Members | 1,769 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,963 developers. It's quick & easy.

How to change default printer while printing document using 'process' ?

Hi..I could not change default printer using C#..In this code, the user can select a printer and a folder to print the files within a folder to selected printer..But when the code can print only to the default printer.. so I need to change the default printer after the user selects the printer.. Thanks in advance !!

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.Diagnostics;
  11. using System.IO;
  12. using System.Drawing.Printing;
  13. using System.Management;
  14. namespace Print_folder
  15. {
  16.     public partial class Form1 : Form
  17.     {
  18.         public static string Path = @"C:\Users\AM6846\Desktop\Folder";
  19.  
  20.         public Form1()
  21.         {
  22.             InitializeComponent();
  23.         }
  24.  
  25.         private void button1_Click(object sender, EventArgs e)
  26.         {
  27.             string[] filenames = Directory.GetFiles(@"C:\Users\AM6846\Desktop\Folder\" + listBox1.SelectedItem);
  28.  
  29.             // Checks if User selects before clicking on print button. 
  30.             // If user clicks on print button directly without selecting an option, app returns an message. 
  31.  
  32.             if (comboBox1.SelectedIndex != -1)
  33.             {
  34.                 if (listBox1.SelectedIndex != -1)
  35.                 {
  36.  
  37.                     try
  38.                     {
  39.                         Process printJob = new Process();
  40.                         foreach (string testString in filenames)
  41.                         {
  42.  
  43.                             printJob.StartInfo.FileName = testString;
  44.                             printJob.StartInfo.UseShellExecute = true;
  45.                             printJob.StartInfo.Verb = "print";
  46.                             printJob.StartInfo.Arguments = comboBox1.SelectedText;
  47.                             printJob.Start();
  48.                             printJob.WaitForInputIdle();
  49.  
  50.                         }
  51.  
  52.                     }
  53.  
  54.                     catch (System.Exception exception)
  55.                     {
  56.  
  57.                         MessageBox.Show(exception.Message);
  58.  
  59.                     }
  60.  
  61.                 }
  62.  
  63.                 else
  64.                 {
  65.  
  66.                     System.Windows.Forms.MessageBox.Show("Please Select a Folder to print!!");
  67.                 }
  68.             }
  69.                 else
  70.                 {
  71.                     System.Windows.Forms.MessageBox.Show("Please Select a printer!!");
  72.                  }
  73.  
  74.         }
  75.  
  76.         private void Form1_Load(object sender, EventArgs e)
  77.         {
  78.             // Enter the path of parent folder.
  79.  
  80.  
  81.             DirectoryInfo dir = new DirectoryInfo(Path);
  82.  
  83.             // Populates the listbox with subfolders.
  84.             this.listBox1.Items.AddRange(dir.GetDirectories());
  85.             textBox1.Text = Path;
  86.  
  87.  
  88.  
  89.             foreach(string strPrinter in PrinterSettings.InstalledPrinters)
  90.             {
  91.  
  92.                 comboBox1.Items.Add(strPrinter);
  93.  
  94.             }
  95.  
  96.         }
  97.  
  98.         private void PopulateInstalledPrintersCombo()
  99.         {
  100.             // Add list of installed printers found to the combo box. 
  101.             // The pkInstalledPrinters string will be used to provide the display string.
  102.             String pkInstalledPrinters;
  103.             for (int i = 0; i < PrinterSettings.InstalledPrinters.Count; i++)
  104.             {
  105.                 pkInstalledPrinters = PrinterSettings.InstalledPrinters[i];
  106.                 comboBox1.Items.Add(pkInstalledPrinters);
  107.             }
  108.         }
  109.  
  110.         private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
  111.         {
  112.  
  113.         }
  114.  
  115.         /*
  116.         public bool SetDefaultPrinter()
  117.         {
  118.             System.Management.ManagementObjectSearcher search =
  119.                            default(System.Management.ManagementObjectSearcher);
  120.             System.Management.ManagementObjectCollection results =
  121.                            default(System.Management.ManagementObjectCollection);
  122.             System.Management.ManagementObject printer =
  123.                            default(System.Management.ManagementObject);
  124.  
  125.  
  126.             search =
  127.              new System.Management.ManagementObjectSearcher("select * from win32_printer");
  128.             results = search.Get();
  129.             //Get Default Printer
  130.             System.Management.ManagementObject defaultPrinter = null;
  131.             foreach (System.Management.ManagementObject foundPrinter in results)
  132.             {
  133.                 System.Management.PropertyDataCollection
  134.                       propertyDataCollection = foundPrinter.Properties;
  135.  
  136.                 if ((bool)foundPrinter["Default"]) // DEFAULT PRINTER
  137.                 {
  138.                     System.Diagnostics.Debug.WriteLine(foundPrinter["Name"]);
  139.                     System.Diagnostics.Debug.WriteLine(foundPrinter["Location"]);
  140.                 }
  141.             }
  142.  
  143.  
  144.             //Sets new default Printer
  145.             foreach (System.Management.ManagementObject foundPrinter in results)
  146.             {
  147.                 System.Diagnostics.Debug.Print(foundPrinter["Name"].ToString());
  148.                 if (foundPrinter["Name"].Equals("PDFCreator"))
  149.                 {
  150.                     System.Management.ManagementBaseObject outParams =
  151.                           foundPrinter.InvokeMethod("SetDefaultPrinter", null, null);
  152.                     if (outParams == null)
  153.                         System.Diagnostics.Debug.WriteLine("Unable to set default printer");
  154.                     Int32 retVal = (int)(uint)outParams.Properties["ReturnValue"].Value;
  155.                     if (retVal == 0)
  156.                         return true;
  157.                     else
  158.                         return false;
  159.                 }
  160.             }
  161.  
  162.             return false;
  163.  
  164.         }
  165.         */
  166.     }
  167. }
  168.  
May 15 '14 #1
4 7340
Luk3r
300 256MB
Expand|Select|Wrap|Line Numbers
  1. printJob.StartInfo.Arguments = comboBox1.SelectedText;
This will return no results. You must step through your code to find out where it's failing to do what you expect it to do. Please use the following syntax (that we went over in your other thread) and let us know how it goes. The problem is, you're passing your printing process a blank argument, so the following code should fix that (assuming that your comboBox houses the names of printers):

Expand|Select|Wrap|Line Numbers
  1. printJob.StartInfo.Arguments = comboBox1.SelectedItem.ToString();
May 15 '14 #2
It's is not working...What i learnt from previous search operation was that the code uses default printer to print the file....so im trying to change the default printer ...Can you help?
Expand|Select|Wrap|Line Numbers
  1. printJob.StartInfo.FileName = testString;
  2.                             printJob.StartInfo.UseShellExecute = true;
  3.                             printJob.StartInfo.Verb = "print";
  4.                             printJob.StartInfo.Arguments = comboBox1.SelectedText;
  5.                             printJob.Start();
  6.                             printJob.WaitForInputIdle();
May 15 '14 #3
Luk3r
300 256MB
As I said, the print process argument that you are passing should be the name of your printer. Examples:
Expand|Select|Wrap|Line Numbers
  1. printJob.StartInfo.Arguments = comboBox1.SelectedItem.ToString();
  2. printJob.StartInfo.Arguments = "Printer_1";
  3. printJob.StartInfo.Arguments = "\\printServer\Printer2"
May 15 '14 #4
okay.. Actually i found way to change the default printer :) , but couldn't print pdf documents though...
Expand|Select|Wrap|Line Numbers
  1.     public static class myPrinters 
  2.           {
  3.           [DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)] 
  4.           public static extern bool SetDefaultPrinter(string Name);
  5.          }
  6.  
  7.         private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
  8.         {
  9.             string pname = this.comboBox1.SelectedItem.ToString();
  10.             myPrinters.SetDefaultPrinter(pname);
  11.         }
  12.  
  13. and include  using System.Runtime.InteropServices;
  14.  
... can you help me with printing pdf documents..
May 15 '14 #5

Post your reply

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

Similar topics

reply views Thread by Brian Boccia | last post: by
7 posts views Thread by DazedAndConfused | last post: by
1 post views Thread by goJoe | last post: by
1 post views Thread by Bharat Parekh | last post: by
1 post views Thread by krithika.sridhar | last post: by
1 post views Thread by rainxy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.