C#中的打印机状态检查 [英] Printer status check in C#

查看:92
本文介绍了C#中的打印机状态检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以给我一些代码如何获取打印机的字符串消息状态。

在我的情况下,我需要检查State并将其与PrintStatus结合并手动编写字符串状态。我不希望这样。我希望现场获得打印机设置状态。



如果离线,纸张接近,纸张末端,自动计算机错误等...

我该怎么做。



我尝试过:



 //获取打印机的ManagementObjectSearcher。 
string query =SELECT * FROM Win32_Printer WHERE Name ='+
cboPrinters.SelectedItem.ToString()+';
ManagementObjectSearcher searcher =
new ManagementObjectSearcher(query);

//获取代表
的ManagementObjectCollection // WMI查询的结果。循环遍历
//单项。显示该项目的某些属性。
foreach(searcher.Get()中的ManagementObject服务)
{
txtName.Text = service.Properties [Name]。Value.ToString();
//这里我想要STRING纸张接近结束,或其他
UInt32 state =(UInt32)service.Properties [PrinterState]。Value;
txtState.Text = GetPropertyValue(service.Properties [PrinterState]);

//这里我想要STRING纸张接近结束,或其他
UInt16 status =(UInt16)service.Properties [PrinterStatus]。Value;
txtStatus.Text = GetPropertyValue(service.Properties [PrinterStatus]); //这里我想要STRIGN纸张近端,或其他

txtDescription.Text = GetPropertyValue(service的.properties [ 描述]);
txtDefault.Text = GetPropertyValue(service.Properties [Default]);
txtHorRes.Text = GetPropertyValue(service.Properties [Horizo​​ntalResolution]);
txtVertRes.Text = GetPropertyValue(service.Properties [VerticalResolution]);
txtPort.Text = GetPropertyValue(service.Properties [PortName]);

lstPaperSizes.Items.Clear();
string [] paper_sizes =(string [])service.Properties [PrinterPaperNames]。Value;
foreach(paper_sizes中的字符串paper_size)
{
lstPaperSizes.Items.Add(paper_size);
}

//列出可用的属性。
foreach(service.Properties中的PropertyData数据)
{
string txt = data.Name;
if(data.Value!= null)
txt + =:+ data.Value.ToString();
Console.WriteLine(txt);
}
}

解决方案

我不知道你是否可以使用任何其中,这是我多年前写的一篇专门针对打印机的文章。你可能会发现一些有用的东西:



保持您的喷墨打印头清洁 [ ^ ]



此问题之前也有人问过。这是另一篇CP文章的链接:



C#中的打印机状态 [ ^ ]

Can someone give me some code how to get string message status of printer.
In my case i need to inspect State and combine that with PrintStatus and that to manually write the string for status. I don't want that. I want to get status of printer setting live.

If its offline, paper near end, paper end, auto cuter error etc...
How do i do that.

What I have tried:

// Get a ManagementObjectSearcher for the printer.
            string query = "SELECT * FROM Win32_Printer WHERE Name='" +
                cboPrinters.SelectedItem.ToString() + "'";
            ManagementObjectSearcher searcher =
                new ManagementObjectSearcher(query);

            // Get the ManagementObjectCollection representing
            // the result of the WMI query. Loop through its
            // single item. Display some of that item's properties.
            foreach (ManagementObject service in searcher.Get())
            {
                txtName.Text = service.Properties["Name"].Value.ToString();
                //here i want STRING "Paper near end", or something else
                UInt32 state = (UInt32)service.Properties["PrinterState"].Value;
                txtState.Text = GetPropertyValue(service.Properties["PrinterState"]); 

                //here i want STRING "Paper near end", or something else
                UInt16 status = (UInt16)service.Properties["PrinterStatus"].Value;
                txtStatus.Text = GetPropertyValue(service.Properties["PrinterStatus"]);//here i want STRIGN "Paper near end", or something else

                txtDescription.Text = GetPropertyValue(service.Properties["Description"]);
                txtDefault.Text = GetPropertyValue(service.Properties["Default"]);
                txtHorRes.Text = GetPropertyValue(service.Properties["HorizontalResolution"]);
                txtVertRes.Text = GetPropertyValue(service.Properties["VerticalResolution"]);
                txtPort.Text = GetPropertyValue(service.Properties["PortName"]);

                lstPaperSizes.Items.Clear();
                string[] paper_sizes = (string[])service.Properties["PrinterPaperNames"].Value;
                foreach (string paper_size in paper_sizes)
                {
                    lstPaperSizes.Items.Add(paper_size);
                }

                // List the available properties.
                foreach (PropertyData data in service.Properties)
                {
                    string txt = data.Name;
                    if (data.Value != null)
                        txt += ": " + data.Value.ToString();
                    Console.WriteLine(txt);
                }
            }

解决方案

I don't know if you'll be able to make use of any of this, but here's an article I wrote a number of years ago that deals specifically with the printer. You might find something useful:

Keep Your InkJet Print Head Clean[^]

This question has also been asked here before. Here's a link to another CP article:

Printer Status in C#[^]


这篇关于C#中的打印机状态检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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