Public g_PrintSet As New System.Drawing.Printing.PrinterSettings
Public g_PageSet As New System.Drawing.Printing.PageSettings
'printer是打印机的名字或者打印机的网络地址,都可以。因为System.Drawing.Printing.PrinterSettings.InstalledPrinters中保存了所有在机器上安装的打印机
For iIndex = 0 To System.Drawing.Printing.PrinterSettings.InstalledPrinters.Count - 1
if System.Drawing.Printing.PrinterSettings.InstalledPrinters.Item(iIndex).Contains(printer) Then
MessageBox.Show(System.Drawing.Printing.PrinterSettings.InstalledPrinters.Item(iIndex))
g_PrintSet.PrinterName = System.Drawing.Printing.PrinterSettings.InstalledPrinters.Item(iIndex)
End If
Next iIndex
g_Print.PrinterSettings = g_PrintSet
设置defaultprinter
Imports System.Management
'SUB 中
Dim gManagementObject As New ManagementObject
gManagementObject.Scope = New ManagementScope(String.Format("//{0}/root/cimv2",computerName))
Dim gSearcher As New ManagementObjectSearcher(String.Format("SELECT DeviceID FROM {0}","Win32_Printer"))
gSearcher.Scope = gManagementObject.Scope
Try
For Each mManagementObject As ManagementObject In gSearcher.Get()
If mManagementObject.Properties.Item("DeviceID").Value.ToString.Equals(PrinterName) Then
mManagementObject.InvokeMethod("SetDefaultPrinter",Nothing)
Exit For
End If
Next
Catch ex As Exception
Console.WriteLine(ex.Message)
Finally
gSearcher.Dispose()
gManagementObject.Dispose()
End Try
如果你是用的Crystal report,
Rpt.PrintOptions.PrinterName = PrinterName
Rpt.PrintToPrinter(1,False,0)
获取计算机名
SystemInformation.ComputerName