語言

以指令碼工作尋找安裝的印表機

由 Integration Services 封裝轉換的資料通常有列印的報表,做為其最終的目的地。 在 Microsoft .NET Framework 中的 System.Drawing.Printing 命名空間提供使用印表機的類別。

[!附註]

如果您想要建立可更輕鬆地在多個封裝之間重複使用的工作,請考慮使用此指令碼工作範例中的程式碼做為自訂工作的起點。 如需詳細資訊,請參閱<開發自訂工作>。

說明

下列範例找到安裝在伺服器上支援 Legal Size 紙張 (用於美國) 的印表機。 檢查支援紙張大小的程式碼是封裝在私用函數中。 為了讓您在指令碼檢查每台印表機的設定時,追蹤指令碼的進度,指令碼使用 Log 方法,來針對使用 Legal Size 紙張的印表機引發參考用訊息,並為沒有 Legal Size 紙張的印表機引發警告。 當您在設計工具中執行封裝時,這些訊息會出現在 Microsoft Visual Studio Tools for Applications (VSTA) IDE 中的 [輸出] 視窗中。

若要設定此指令碼工作範例

  1. 建立名為 PrinterList 且類型為 Object 的變數。

  2. 在 [指令碼工作編輯器] 的 [指令碼] 頁面上,將此變數加入 ReadWriteVariables 屬性。

  3. 在指令碼專案中,加入 System.Drawing 命名空間的參考。

  4. 在您的程式碼中,使用 Imports 陳述式匯入 System.Collections 與 System.Drawing.Printing 命名空間。

程式碼

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 = ScriptResults.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
public void Main()
        {

            PrinterSettings currentPrinter = new PrinterSettings();
            PaperSize size;
            Boolean Flag = false;

            ArrayList printerList = new ArrayList();
            foreach (string printerName in PrinterSettings.InstalledPrinters)
            {
                currentPrinter.PrinterName = printerName;
                if (PrinterHasLegalPaper(currentPrinter))
                {
                    printerList.Add(printerName);
                    Dts.Events.FireInformation(0, "Example", "Printer " + printerName + " has legal paper.", String.Empty, 0, ref Flag);
                }
                else
                {
                    Dts.Events.FireWarning(0, "Example", "Printer " + printerName + " DOES NOT have legal paper.", String.Empty, 0);
                }
            }

            Dts.Variables["PrinterList"].Value = printerList;

            Dts.TaskResult = (int)ScriptResults.Success;

        }

        private bool PrinterHasLegalPaper(PrinterSettings thisPrinter)
        {

            bool hasLegal = false;

            foreach (PaperSize size in thisPrinter.PaperSizes)
            {
                if (size.Kind == PaperKind.Legal)
                {
                    hasLegal = true;
                }
            }

            return hasLegal;

        }
Integration Services 圖示 (小) 掌握 Integration Services 的最新狀態

若要取得 Microsoft 的最新下載、文件、範例和影片以及社群中的選定解決方案,請瀏覽 MSDN 上的 Integration Services 頁面:


若要得到這些更新的自動通知,請訂閱該頁面上所提供的 RSS 摘要。

請參閱

概念

指令碼工作範例