从WMI和作为Windows服务的主机获取打印机属性 [英] Getting printer properties from WMI and host as windows service

查看:71
本文介绍了从WMI和作为Windows服务的主机获取打印机属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我在将打印机默认属性从WMI投入服务时遇到问题.我使用的代码如下,它可以完美地用作控制台应用程序.当我将其作为Windows服务托管时,将永远不会返回默认打印机.该值始终为false.

Hi All,

I have a problem in getting printer default property from WMI into service. The code I have used is as follows and it works perfectly as a console application. When I host it as windows service, the default printer is never returned. The value is always false.

static void Main(string[] args)
        {
            ConnectionOptions objConnection = new ConnectionOptions();
            
            string strMachineName = System.Environment.MachineName;
            ManagementScope objScope = new ManagementScope(@"\\" + strMachineName + @"\root\cimv2");//, objConnection);
            objScope.Connect();
            SelectQuery selectQuery = new SelectQuery();
            selectQuery.QueryString = "Select * from win32_Printer";
            ManagementObjectSearcher MOS = new ManagementObjectSearcher(objScope, selectQuery);
            ManagementObjectCollection MOC = MOS.Get();
            foreach (ManagementObject mo in MOC)
            {
                Console.WriteLine(MOC.Count.ToString());
                Console.WriteLine(mo["Name"].ToString().ToUpper());
                if ((bool)mo["Default"])
                {
                    Console.WriteLine("This is Default printer");
                }
            }
            Console.ReadLine();
        }



我希望我的项目能早日得到答复.

Surya



I am expecting an early reply for my project.

Surya

推荐答案

您可以期望早日得到所有答复.当我们有时间时,您会在我们的时间得到一份.我们都是志愿者,在这里贡献自己的时间来帮助其他人.要求我们首先看您的问题只是不礼貌.

它不起作用,因为该服务作为本地系统运行,并且您与WMI的连接也在运行,而不是登录的用户.即使您要打开允许与桌面交互",您仍然可以无法为用户获取默认打印机.

即使没有人登录,服务也会运行,那么您将如何为不在那里的人获得打印机?
You can expect an early reply all you want. You''ll get one on our time, when we have time. We''re all volunteers here, donating our time to help other people. Demanding we look at your problem first is just plain rude.

It doesn''t work because the service is running as the Local System and so is your connection to WMI, not the user that''s logged in. Even if you were to turn on "Allow interaction with Desktop", you still can''t get the default printer for the user.

Services run even if noone is logged in, so how are you going to get the printers for someone who isn''t there?


对不起,戴夫.我正在locahost中运行该服务.但是仍然没有获得默认的打印机名称.
Sorry Dave for my urgency. I am running the service in locahost. But still it is not getting the default printer name.


这篇关于从WMI和作为Windows服务的主机获取打印机属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆