C# 是否有方法确定Microsoft XPS Document Writer在系统上是否可用和正常工作

C# 是否有方法确定Microsoft XPS Document Writer在系统上是否可用和正常工作,c#,.net,xps,C#,.net,Xps,是否有可靠的方法通过.Net确定Microsoft XPS Document Writer在系统上是否可用和正常工作 此外,XPS编写器的名称是否在所有Windows发行版(如英语、德语(…)上)上都相同 XPS Writer在Vista之后的所有Windows系统上都是绝对可用的。同样在入门版、所有x86和x64版以及Windows 8上?我不知道名称,但“打印机”的型号也是Microsoft XPS Document Writer,并且将保持不变 你可以去找那种型号的打印机 看一看 我怀疑您

是否有可靠的方法通过.Net确定Microsoft XPS Document Writer在系统上是否可用和正常工作

此外,XPS编写器的名称是否在所有Windows发行版(如英语、德语(…)上)上都相同


XPS Writer在Vista之后的所有Windows系统上都是绝对可用的。同样在入门版、所有x86和x64版以及Windows 8上?

我不知道名称,但“打印机”的型号也是Microsoft XPS Document Writer,并且将保持不变

你可以去找那种型号的打印机

看一看

我怀疑您可以使用下面的代码片段尝试打印XPS,如果它不工作,您可能没有打印机

            try
            {
                // Print the Xps file while providing XPS validation and progress notifications.
                PrintSystemJobInfo xpsPrintJob = defaultPrintQueue.AddJob(f.Name, nextFile, false);
            }
            catch (PrintJobException e)
            {
                Console.WriteLine("\n\t{0} could not be added to the print queue.", f.Name);
                if (e.InnerException.Message == "File contains corrupted data.")
                {
                    Console.WriteLine("\tIt is not a valid XPS file. Use the isXPS Conformance Tool to debug it.");
                }
                Console.WriteLine("\tContinuing with next XPS file.\n");
            }

正如Pieter Witvoet所建议的,如果安装了XPSPrinter或未安装XPSPrinter,则根据驱动程序名称返回一个方法

该方法在打印机中循环,直到找到一个,或者扫描每一个,但没有找到一个。 需要在项目中添加对“系统管理”的引用

    private bool GetIfXPSPrinterIsInstalled()
    {
        bool isXPSPrinterMissing = true;
        try
        {               
            var printerQuery = new System.Management.ManagementObjectSearcher("SELECT * from Win32_Printer");
            var iterator = printerQuery.Get().GetEnumerator();
            while (iterator.MoveNext() && isXPSPrinterMissing )
            {
                //isXPSPrinterMissing = iterator.Current.GetPropertyValue("DriverName").ToString() != "Microsoft XPS Document Writer";
                isXPSPrinterMissing = !iterator.Current.GetPropertyValue("DeviceID").ToString().ToUpper().Contains("XPS");
            }
            if (isXPSPrinterMissing )
            {
                MessageBox.Show("Warning, there is no XPS printer installed on this computer");
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("System couldn't verify if there is a XPS printer installed because an error occured");             
        }
        return !isXPSPrinterMissing;
    }

编辑:我发现驱动程序名有时可能会出错。对于XPS打印机和其他一些非XPS打印机,它可能是“远程桌面轻松打印”。因此,检查DeviceID是否包含XPS是一种更安全的方法。

名称绝对不可靠。用户可以随时重命名“Microsoft XPS Document Writer”打印队列。看起来模型实际上是打印机驱动程序的名称,可以通过PrintQueue.QueueDriver.name获得。