获取所有打印机相关信息



public Map printerMap()
{
    Microsoft.Dynamics.AX.Framework.Reporting.Shared.PrinterHelper printerHelper;
    Microsoft.Dynamics.AX.Framework.Reporting.Shared.PrinterInfo printerInfo;
    System.Collections.ArrayList names;
    System.Collections.IEnumerator enumerator;

    Map      printerMap;

    str name;
    int printerStatus;
    str driverName;
    str portName;
    str comments;
    int jobCountSinceLastReset;

    if(!printerMap)
    {
        printerMap = new Map(Types::String, Types::Container);
        printerHelper = new Microsoft.Dynamics.AX.Framework.Reporting.Shared.PrinterHelper();
        names = printerHelper.get_PrinterNames();
        if (names != null)
        {
            enumerator = names.GetEnumerator();
            while (enumerator.MoveNext())
            {
                name = enumerator.get_Current();
                printerInfo = printerHelper.GetPrinterInfo(name);
                printerStatus           = CLRInterop::getAnyTypeForObject(printerInfo.get_PrinterStatus());
                driverName              = CLRInterop::getAnyTypeForObject(printerInfo.get_DriverName());
                portName                = CLRInterop::getAnyTypeForObject(printerInfo.get_PortName());
                comments                = CLRInterop::getAnyTypeForObject(printerInfo.get_Comment());
                jobCountSinceLastReset  = CLRInterop::getAnyTypeForObject(printerInfo.get_JobCountSinceLastReset());

                printerMap.insert(name, [name, printerStatus, driverName, portName, comments, jobCountSinceLastReset]);
            }
        }
    }

    return printerMap;
}


另外的方式:

static void getPrinterName(Args _args)
{
    //获得默认打印机
    PrintJobSettings printMe = new PrintJobSettings();
    print printMe.deviceName();
    pause;
   
    // 获得当前机器所有的发打印机列表
    printJobSettings pjs;
    str printer;
    int i;
    pjs = new printJobSettings();
    for (i=1; i<=pjs.GetNumberOfPrinters(); i++)
    {
        printer = pjs.GetPrinter(i);
        pjs.DeviceName(printer);
        print "打印机 ",i, " 名称 ",  printer;
    }
    pause;
}

在AX中打印Txt文件

static void KlFor_PrintUsingCopy(Args _args)
{
    // use only for text files

    #File
    Set     permissionSet;
    System.Exception interopException;
    FileName    destinationFileName;

    // parameters
    Filename    fileName    = @"C:\text.txt";
    PrinterName printername = @"\\PrintServer\YourPrinter";
    ;

    try
    {
        // combine the destination path using the printer and the filename
        new InteropPermission(InteropKind::ClrInterop).assert();
        destinationFileName = System.IO.Path::Combine(printerName, System.IO.Path::GetFileName(fileName));
        CodeAccessPermission::revertAssert();

        // then copy the file
        permissionSet =  new Set(Types::Class);
        permissionSet.add(new FileIOPermission(fileName,#io_write));
        permissionSet.add(new FileIOPermission(destinationFileName,#io_write));
        permissionSet.add(new InteropPermission(InteropKind::ClrInterop));

        CodeAccessPermission::assertMultiple(permissionSet);

        System.IO.File::Copy(fileName, destinationFileName);

        CodeAccessPermission::revertAssert();
    }
    catch(Exception::CLRError)
    {
        // catch CLR errors and show inner exception
        interopException = CLRInterop::getLastException();
        while (!CLRInterop::isNull(interopException.get_InnerException()))
        {
            interopException = interopException.get_InnerException();
        }

        error(strFmt("%1", CLRInterop::getAnyTypeForObject(interopException.get_Message())));
    }
}

在AX中打印任何可打印的文件:

static void KlFor_PrintUsingSystemDiagnostics(Args _args)
{
    #File
    System.Diagnostics.ProcessStartInfo processInfo;
    System.Diagnostics.Process process;
    System.Exception interopException;
   
    // Parameters
    Filename    fileName    = @"C:\text.txt";
    PrinterName printername = @"\\PrintServer\YourPrinter";
    ;

    printerName = '"' + printerName + '"';

    try
    {
        // assert permissions
        new InteropPermission(InteropKind::ClrInterop).assert();

        process = new System.Diagnostics.Process();

        processInfo = process.get_StartInfo();

        processInfo.set_UseShellExecute(true);
        processInfo.set_CreateNoWindow(true);
        processInfo.set_FileName(fileName);
       
        // the argument is the printer name
        processInfo.set_Arguments(printerName);
       
        // set the verb to printto
        processInfo.set_Verb('printto');
        processInfo.set_WindowStyle(System.Diagnostics.ProcessWindowStyle::Hidden);

        process.Start();

        // revert asserted permissions
        CodeAccessPermission::revertAssert();
    }
    catch(Exception::CLRError)
    {
        interopException = CLRInterop::getLastException();
        while (!CLRInterop::isNull(interopException.get_InnerException()))
        {
            interopException = interopException.get_InnerException();
        }

        error(strFmt("@RDS10563", fileName, CLRInterop::getAnyTypeForObject(interopException.get_Message())));
    }
}

或者:

static void KlFor_GetAllVerbsForFile2(Args _args)
{
    System.Diagnostics.ProcessStartInfo processInfo;
    System.Array verbs;
    int i;
    List fileNames;
    container verbsContainer;
    ListEnumerator listEnumerator;

    fileNames = new List(Types::String);
    fileNames.addEnd(".xlsx");
    fileNames.addEnd(".docx");
    fileNames.addEnd(".png");
    fileNames.addEnd(".txt");
    fileNames.addEnd(".pdf");

    listEnumerator = fileNames.getEnumerator();

    new InteropPermission(InteropKind::ClrInterop).assert();

    while(listEnumerator.moveNext())
    {
        processInfo = new System.Diagnostics.ProcessStartInfo(listEnumerator.current());
        verbs = processInfo.get_Verbs();

        verbsContainer = conNull();

        for( i=0; i<ClrInterop::getAnyTypeForObject(verbs.get_Length()); i++ )
        {
            verbsContainer += ClrInterop::getAnyTypeForObject(verbs.GetValue(i));
        }

        info(strFmt("%1 has verbs: %2", listEnumerator.current(), con2Str(verbsContainer)));
    }

    CodeAccessPermission::revertAssert();
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值