PrinterSettings.PrinterName Propriedade
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
Obtém ou define o nome da impressora a usar.
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
Valor de Propriedade
O nome da impressora a usar.
Exemplos
O seguinte exemplo de código especifica a impressora alvo definindo a PrinterName propriedade e, se for IsValidtrue, imprime o documento na impressora especificada. O exemplo tem três pré-requisitos:
Uma variável nomeada
filePathfoi definida para o caminho do ficheiro a imprimir.Foi definido um método chamado
pd_PrintPage, que gere o PrintPage evento.Uma variável nomeada
printerfoi definida para o nome da impressora.
Use os System.Drawingnamespaces , System.Drawing.Printing, e System.IO para este exemplo.
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
Observações
Depois de definir o nome da impressora, ligue IsValid para determinar se o nome da impressora é reconhecido como uma impressora válida no sistema.
Também pode usar a InstalledPrinters propriedade para obter uma lista de impressoras instaladas no sistema.