PrinterSettings.PrinterName 属性

定义

获取或设置要使用的打印机的名称。Gets or sets the name of the printer to use.

public:
 property System::String ^ PrinterName { System::String ^ get(); void set(System::String ^ value); };
public string PrinterName { get; set; }
member this.PrinterName : string with get, set
Public Property PrinterName As String

属性值

String

要使用的打印机的名称。The name of the printer to use.

示例

下面的代码示例通过设置属性来指定目标打印机 PrinterName ,如果 IsValid 为,则在 true 指定的打印机上打印文档。The following code 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 has three prerequisites:

  • 已将名为的变量 filePath 设置为要打印的文件的路径。A variable named filePath has been set to the path of the file to print.

  • 已经定义了一个名为的方法 pd_PrintPage ,该方法处理 PrintPage 事件。A method named pd_PrintPage, which handles the PrintPage event, has been defined.

  • 已将名为的变量 printer 设置为打印机的名称。A variable named printer has been set to the printer's name.

System.Drawing 此示例中使用、 System.Drawing.PrintingSystem.IO 命名空间。Use the System.Drawing, System.Drawing.Printing, and System.IO namespaces for this example.

public:
   void Printing( String^ printer )
   {
      try
      {
         streamToPrint = gcnew StreamReader( filePath );
         try
         {
            printFont = gcnew System::Drawing::Font( "Arial",10 );
            PrintDocument^ pd = gcnew PrintDocument;
            pd->PrintPage += gcnew PrintPageEventHandler(
               this, &Form1::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 );
      }
   }
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);
  }
}

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
   

注解

设置打印机名称后,调用 IsValid 来确定是否将打印机名称识别为系统上的有效打印机。After setting the printer name, call IsValid to determine if the printer name is recognized as a valid printer on the system.

你还可以使用 InstalledPrinters 属性来获取安装在系统上的打印机的列表。You can also use the InstalledPrinters property to get a list of printers installed on the system.

适用于

另请参阅