共用方式為


Finding Installed Printers with the Script Task

The data that is transformed by Integration Services packages often has a printed report as its final destination. The System.Drawing.Printing namespace in the Microsoft .NET Framework provides classes for working with printers.

ms135995.note(zh-tw,SQL.90).gif附註:
If you want to create a task that you can more easily reuse across multiple packages, consider using the code in this Script task sample as the starting point for a custom task. For more information, see Developing a Custom Task.

Description

The following example locates printers installed on the server that support legal size paper (as used in the United States). The code to check supported paper sizes is encapsulated in a private function. To enable you to track the progress of the script as it checks the settings for each printer, the script uses the Log method to raise an informational message for printers with legal size paper, and to raise a warning for printers without legal size paper. These messages are displayed in the Output window of the VSA IDE when you run the package in the designer.

To configure this Script Task example

  1. Create the variable named PrinterList with type Object.

  2. On the Script page of the Script Task Editor, add this variable to the ReadWriteVariables property.

  3. In the script project, add a reference to the System.Drawing namespace.

  4. In your code, use Imports statements to import the System.Collections and the System.Drawing.Printing namespaces.

Code

Public Sub Main()

    Dim printerName As String
    Dim currentPrinter As New PrinterSettings
    Dim size As PaperSize

    Dim printerList As New ArrayList
    For Each printerName In PrinterSettings.InstalledPrinters
        currentPrinter.PrinterName = printerName
        If PrinterHasLegalPaper(currentPrinter) Then
            printerList.Add(printerName)
            Dts.Events.FireInformation(0, "Example", _
                "Printer " & printerName & " has legal paper.", _
                String.Empty, 0, False)
        Else
            Dts.Events.FireWarning(0, "Example", _
                "Printer " & printerName & " DOES NOT have legal paper.", _
                String.Empty, 0)
        End If
    Next

    Dts.Variables("PrinterList").Value = printerList

    Dts.TaskResult = Dts.Results.Success

End Sub

Private Function PrinterHasLegalPaper( _
    ByVal thisPrinter As PrinterSettings) As Boolean

    Dim size As PaperSize
    Dim hasLegal As Boolean = False

    For Each size In thisPrinter.PaperSizes
        If size.Kind = PaperKind.Legal Then
            hasLegal = True
        End If
    Next

    Return hasLegal

End Function