语言

使用脚本任务查找已安装的打印机

由 Integration Services 包转换的数据的最终目的通常是打印成一份报表。 Microsoft .NET Framework 中的 System.Drawing.Printing 命名空间提供了用于处理打印机的类。

注意注意

如果希望创建可更方便地重用于多个包的任务,请考虑以此脚本任务示例中的代码为基础,创建自定义任务。 有关详细信息,请参阅开发自定义任务。

说明

以下示例查找服务器上安装的支持合法纸张大小(以在美国使用为例)的打印机。 用于检查所支持的纸张大小的代码封装在一个私有函数中。 为了能够跟踪脚本检查每个打印机设置的进度,脚本使用 Log 方法对具有合法纸张大小的打印机引发信息性消息,对不具有合法纸张大小的打印机引发警告。 在设计器中运行包时,这些消息显示在 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 源。

请参阅

概念

脚本任务示例