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

適用於:Azure Data Factory 中的 SQL Server SSIS Integration Runtime

由 Integration Service 套件轉換的資料通常會以列印報表為其最終目的地。 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.CollectionsSystem.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;  
  
        }  

另請參閱

指令碼工作範例