This documentation is archived and is not being maintained.

PrinterSettings.PrinterName Property

Gets or sets the name of the printer to use.

[Visual Basic]
Public Property PrinterName As String
[C#]
public string PrinterName {get; set;}
[C++]
public: __property String* get_PrinterName();
public: __property void set_PrinterName(String*);
[JScript]
public function get PrinterName() : String;
public function set PrinterName(String);

Property Value

The name of the printer to use.

Remarks

After setting the printer name, call IsValid to determine if the printer name is recognized as a valid printer on the system.

You can also use the InstalledPrinters property to get a list of printers installed on the system.

Example

[Visual Basic, C#, C++] The following example specifies the target printer by setting the PrinterName property, and if the IsValid is true, prints the document on the specified printer. The example makes three assumptions: that a variable named filePath has been set to the path of the file to print; that a method named pd_PrintPage, which handles the PrintPage event, has been defined; and that a variable named printer has been set to the printer's name.

[Visual Basic, C#, C++] Use the System.Drawing, System.Drawing.Printing, and System.IO namespaces for this example.

[Visual Basic] 
Public Sub Printing(printer As String)
    Try
        streamToPrint = New StreamReader(filePath)
        Try
            printFont = New Font("Arial", 10)
            Dim pd As New PrintDocument()
            AddHandler pd.PrintPage, AddressOf pd_PrintPage
            ' Specify the printer to use.
            pd.PrinterSettings.PrinterName = printer

            If pd.PrinterSettings.IsValid then
               pd.Print()
            Else
               MessageBox.Show("Printer is invalid.")
            End If
        Finally
            streamToPrint.Close()
        End Try
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub
   

[C#] 
public void Printing(string printer) {
  try {
    streamToPrint = new StreamReader (filePath);
    try {
      printFont = new Font("Arial", 10);
      PrintDocument pd = new PrintDocument(); 
      pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
      // Specify the printer to use.
      pd.PrinterSettings.PrinterName = printer;

      if (pd.PrinterSettings.IsValid) {
         pd.Print();
      } 
      else {    
         MessageBox.Show("Printer is invalid.");
      }
    } 
    finally {
      streamToPrint.Close();
    }
  } 
  catch(Exception ex) {
    MessageBox.Show(ex.Message);
  }
}


[C++] 
public:
 void Printing(String* printer) {
   try {
     streamToPrint = new StreamReader (filePath);
     try {
       printFont = new System::Drawing::Font(S"Arial", 10);
       PrintDocument* pd = new PrintDocument(); 
       pd->PrintPage += new PrintPageEventHandler(this, &Form1::pd_PrintPage);
       // Specify the printer to use.
       pd->PrinterSettings->PrinterName = printer;

       if (pd->PrinterSettings->IsValid) {
          pd->Print();
       } 
       else {    
          MessageBox::Show(S"Printer is invalid.");
       }
     } 
     __finally {
       streamToPrint->Close();
     }
   } 
   catch(Exception* ex) {
     MessageBox::Show(ex->Message);
   }
 }
 

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

.NET Framework Security: 

See Also

PrinterSettings Class | PrinterSettings Members | System.Drawing.Printing Namespace | IsValid | InstalledPrinters | Duplex | MaximumCopies

Show: