如何检查是否有默认打印机(Windows)?

时间:2010-08-13 06:37:09

标签: java .net windows delphi winapi

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

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

3 个答案:

答案 0 :(得分:3)

在.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

答案 1 :(得分:3)

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

PrintService defaultPrinter = PrintServiceLookup.lookupDefaultPrintService();

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


旧答案

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

进一步阅读:

答案 2 :(得分: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();
        }
    }
}