【问题标题】:How can I check if there is a default printer (Windows)?如何检查是否有默认打印机 (Windows)?
【发布时间】:2011-03-29 07:36:50
【问题描述】:

我是否可以从应用程序(本机、Java 或 .Net)中使用 API 或注册表项来检查当前登录的用户是否配置了默认打印机?

更新:非常感谢到目前为止的答案!根据知识库文章http://support.microsoft.com/kb/156212,注册表项(读/写)仅记录到 Windows 2000。在较新版本中是否有用于本机访问的 Win API 方法?

【问题讨论】:

    标签: java .net windows delphi winapi


    【解决方案1】:

    在 .NET 中,此代码适用于我:

    public static string DefaultPrinterName()
    {
      string functionReturnValue = null;
      System.Drawing.Printing.PrinterSettings oPS 
        = new System.Drawing.Printing.PrinterSettings();
    
      try
      {
        functionReturnValue = oPS.PrinterName;
      }
      catch (System.Exception ex)
      {
        functionReturnValue = "";
      }
      finally
      {
        oPS = null;
      }
      return functionReturnValue;
    }
    

    来自: http://in.answers.yahoo.com/question/index?qid=20070920032312AAsSaPx

    【讨论】:

    • 真的需要oPS = null吗?
    • 不,我认为不是——我从链接中复制了代码而没有更改它(仅用于编译并检查它是否有效)
    【解决方案2】:

    有一个 Java API 可以获取默认打印机:

    PrintService defaultPrinter = PrintServiceLookup.lookupDefaultPrintService();
    

    如果没有默认打印机或服务,则返回 null。这可以作为一个测试。


    旧答案

    这些信息可以在注册表中找到。您无法使用纯 Java 访问注册表,但有针对此问题的 JNDI 解决方案。所以基本上你必须测试注册表中是否存在某个键。而且,作为奖励,如果您到目前为止,您甚至应该能够获得默认打印机的名称:)

    进一步阅读:

    【讨论】:

    • 投反对票,因为有非常好的 API 可以确定此信息,而无需诉诸不受支持的注册表黑客(知识库文章中的信息声称仅支持 Windows 2000)。
    • @Larry - Java 中完美的 API?请随意举个例子。我喜欢学习新东西。
    • OP 要求 .net 或 native 作为选项。我希望 Java 有打印 API,不是吗?
    • @Larry - 感谢您的评论 - 我研究了 Java,看起来 Java 支持检测默认打印机。改变了我的答案。
    【解决方案3】:

    非托管Print Spooler API winspool.drv 中有一个函数。您可以调用GetDefaultPrinter函数返回默认打印机的名称。

    这是非托管函数的 P/Invoke 签名:

    [DllImport("winspool.drv", CharSet=CharSet.Auto, SetLastError=true)]
    private static extern bool GetDefaultPrinter(
        StringBuilder buffer,
        ref int bufferSize);
    

    使用该函数判断是否设置了默认打印机:

        public static bool IsDefaultPrinterAssigned()
        {
            //initialise size at 0, used to determine size of the buffer
            int size = 0;
    
            //for first call provide a null StringBuilder and 0 size to determine buffer size
            //return value will be false, as the call actually fails internally setting the size to the size of the buffer
            GetDefaultPrinter(null, ref size);
    
            if (size != 0)
            {
                //default printer set
                return true;
            }
    
            return false;
        }
    

    使用该函数返回默认的打印机名称,如果没有设置默认则返回空字符串:

        public static string GetDefaultPrinterName()
        {
            //initialise size at 0, used to determine size of the buffer
            int size = 0;
    
            //for first call provide a null StringBuilder and 0 size to determine buffer size
            //return value will be false, as the call actually fails internally setting the size to the size of the buffer
            GetDefaultPrinter(null, ref size);
    
            if (size == 0)
            {
                //no default printer set
                return "";
            }
    
            StringBuilder printerNameStringBuilder = new StringBuilder(size);
    
            bool success = GetDefaultPrinter(printerNameStringBuilder, ref size);
    
            if (!success)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }
    
            return printerNameStringBuilder.ToString();
        }
    

    在控制台应用程序中测试的完整代码:

    using System;
    using System.ComponentModel;
    using System.Runtime.InteropServices;
    using System.Text;
    
    namespace DefaultPrinter
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine(IsDefaultPrinterAssigned());
                Console.WriteLine(GetDefaultPrinterName());
                Console.ReadLine();
            }
    
            [DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)]
            private static extern bool GetDefaultPrinter(
                StringBuilder buffer,
                ref int bufferSize);
    
            public static bool IsDefaultPrinterAssigned()
            {
                //initialise size at 0, used to determine size of the buffer
                int size = 0;
    
                //for first call provide a null StringBuilder to and 0 size to determine buffer size
                //return value will be false, as the call actually fails internally setting the size to the size of the buffer
                GetDefaultPrinter(null, ref size);
    
                if (size != 0)
                {
                    //default printer set
                    return true;
                }
    
                return false;
            }
    
            public static string GetDefaultPrinterName()
            {
                //initialise size at 0, used to determine size of the buffer
                int size = 0;
    
                //for first call provide a null StringBuilder to and 0 size to determine buffer size
                //return value will be false, as the call actually fails internally setting the size to the size of the buffer
                GetDefaultPrinter(null, ref size);
    
                if (size == 0)
                {
                    //no default printer set
                    return "";
                }
    
                StringBuilder printerNameStringBuilder = new StringBuilder(size);
    
                bool success = GetDefaultPrinter(printerNameStringBuilder, ref size);
    
                if (!success)
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error());
                }
    
                return printerNameStringBuilder.ToString();
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-14
      • 1970-01-01
      • 2017-07-07
      • 1970-01-01
      相关资源
      最近更新 更多