如何查看我的打印机是否存在c# [英] How to see if my printer exists c#

查看:86
本文介绍了如何查看我的打印机是否存在c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



对不起,让我试试一下我的问题。首先是我当前代码的样本然后我会问我需要问什么。



Hi all,

Sorry let me try improve my question here a bit. First a sample of my current code then i will ask what i need to ask.

private void button2_Click(object sender, EventArgs e)
        {
            string cmdInput;
            string cmdPre;
            int index;
            string item;
            string selectedText;
            string server;
            int index1;
            string item1;


            index = listBox2.SelectedIndex;
            item = listBox2.Items[index].ToString();
            selectedText = item;

            index1 = listBox1.SelectedIndex; ;
            item1 = listBox1.Items[index1].ToString();

            //Creates localprintserver where the queue's can be created on
            LocalPrintServer localPrintServer = new LocalPrintServer();
            PrintQueueCollection myPrintQueues = localPrintServer.GetPrintQueues();
            
            //Need to add the items to a string or array or something so i can check                            if printer exists
            foreach (PrintQueue pq in myPrintQueues)
            {
               // Add items variable
            }


            //string myPrintQueues = PrinterSettings.InstalledPrinters.ToString();

            if (item1.Contains("Solution Worx - HQ"))
            {
                //string myPrintQueues = PrinterSettings.InstalledPrinters.ToString();

                if (myPrintQueues.Contains(selectedText))
                {

                    server = @"\\srvswx.swxhq.local\";

                    cmdPre = @"RunDll32.EXE printui.dll,PrintUIEntry /dn /q /n " + server;
                    cmdInput = selectedText;
                    ProcessStartInfo processStartInfo = new ProcessStartInfo("cmd", "/c " + cmdPre + cmdInput);
                    Process process = Process.Start(processStartInfo);
                }
                else
                {
                    MessageBox.Show("The print queue you have selected is not installed on your PC");
                }
            }





所以这是我的代码。我有两个列表框,左侧是用户选择其位置的列表框,右侧是列表框,用户选择服务器上的打印队列。现在我的问题是,当他们按下删除按钮时,我需要检查该打印机是否存在以及是否存在它会删除打印机,如果没有,则会向他们提供错误信息。很遗憾没有太多关于我想要实现的文档。



So this is my code. I have 2 list boxes the one on the left the users selects his location and the one on the right they select the print queue on the server. Now my problem is when they press the 'remove' button i need to check if that printer exists and if it exists it removes the printer and if not it provides them with an error msg. There is unfortunately not much documentation on what i am trying to achieve.

推荐答案

你应该调试然后使用Watch窗口或其他调试工具来查看你得到的确切内容。但是,以下是可以帮助您的文档:



http://msdn.microsoft.com/en-us/library/system.printing.printqueuecollection(v = vs.110)的.aspx [< a href =http://msdn.microsoft.com/en-us/library/system.printing.printqueuecollection(v=vs.110).aspx\"target =_ blanktitle =New Window> ^ ]并且底部有示例代码。
You should debug and then use the Watch window or other debug tools to see exactly what you get. However, here is the documentation that should help you:

http://msdn.microsoft.com/en-us/library/system.printing.printqueuecollection(v=vs.110).aspx[^] and it has sample code at the bottom.


这篇关于如何查看我的打印机是否存在c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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