在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → C# 域内打印机搜索安装 示例源码

C# 域内打印机搜索安装 示例源码

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:0.49M
  • 下载次数:35
  • 浏览次数:428
  • 发布时间:2018-01-10
  • 实例类别:C#语言基础
  • 发 布 人:sword_liu
  • 文件格式:.zip
  • 所需积分:2
 相关标签: 打印机 安装 打印 搜索

同类人气实例

实例介绍

【实例简介】

【实例截图】

from clipboard

【核心代码】

using System;
using System.DirectoryServices;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Management;
using System.Runtime.InteropServices;

namespace Printer_Tool
{
    public partial class Form1 : Form
    {
        bool rePaint = true;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string filter = "(objectCategory=printQueue)";

            if (textBox1.Text != "" || textBox2.Text != "" || textBox3.Text != "")
            {
                filter = "(&"   filter ;
                if (textBox1.Text != "")
                    filter  = "(printername=*"   textBox1.Text   "*)";
                if (textBox2.Text != "")
                    filter  = "(location=*"   textBox2.Text   "*)";
                if (textBox3.Text != "")
                    filter  = "(drivername=*"   textBox3.Text   "*)";
                filter  = ")";
            }

            DataSet dataSet = new DataSet();

            DirectoryEntry entry = new DirectoryEntry("LDAP://apa.gad.schneider-electric.com/OU=Servers,OU=CN,OU=Countries,DC=apa,DC=gad,DC=schneider-electric,DC=com");
            using (entry)
            {
                using (DirectorySearcher search = new DirectorySearcher(entry, filter, new string[] { "printername", "location", "drivername", "description", "servername", "printsharename" }))
                {
                    search.PageSize = 1000;
                    string[] columns = { "PrinterName", "PrinterLocation", "Model", "Description", "ServerName", "PrintShareName" };
                    DataTable dataTable = dataSet.Tables.Add("Printers");
                    DataRow dataRow;

                    foreach (string colName in columns)
                    {
                        dataTable.Columns.Add(colName);
                    }

                    using (SearchResultCollection resultCollection = search.FindAll()) {
                        toolStripStatusLabel1.Text = string.Format("{0} item(s) found.", resultCollection.Count);
                        foreach (SearchResult result in resultCollection) {
                            dataRow = dataTable.NewRow();
                            
                            foreach (string column in result.Properties.PropertyNames)
                            {
                                switch (column) { 
                                    case "printername":
                                        dataRow[0] = result.Properties[column][0];
                                        break;
                                    case "location":
                                        dataRow[1] = result.Properties[column][0];
                                        break;
                                    case "drivername":
                                        dataRow[2] = result.Properties[column][0];
                                        break;
                                    case "description":
                                        dataRow[3] = result.Properties[column][0];
                                        break;
                                    case "servername":
                                        dataRow[4] = result.Properties[column][0];
                                        break;  
                                    case "printsharename":
                                        dataRow[5] = result.Properties[column][0];
                                        break;
                                }                                   
                            }
                            dataTable.Rows.Add(dataRow);
                        }
                    }
                }
            }
            dataGridView1.DataSource = dataSet.Tables["Printers"];
        }

        private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (e.RowIndex >= 0)
                {
                    if (dataGridView1.Rows[e.RowIndex].Selected == false)
                    {
                        dataGridView1.ClearSelection();
                        dataGridView1.Rows[e.RowIndex].Selected = true;
                    }
                    if (dataGridView1.SelectedRows.Count == 1)
                    {
                        dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
                    }
                    contextMenuStrip1.Show(MousePosition.X, MousePosition.Y);
                }
            }
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            string cmdText = string.Format("explorer \"\\\\{0}\\{1}", dataGridView1.SelectedCells[5].Value.ToString(), dataGridView1.SelectedCells[6].Value.ToString()   "\"");

            Process process = new Process();
            process.StartInfo.FileName = "cmd.exe";
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardInput = true;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardError = true;
            process.StartInfo.CreateNoWindow = true;
            process.Start();

            process.StandardInput.WriteLine(cmdText   "&exit");
            process.StandardInput.AutoFlush = true;

            string result = process.StandardOutput.ReadToEnd();
            process.Close();

            MessageBox.Show(result);
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("Are you sure want to exit the application ?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                e.Cancel = false;
            }
            else
                e.Cancel = true;
        }

        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            if (dataGridView1.HorizontalScrollingOffset==0)
            {
                Image icon = Properties.Resources.printer;
                e.Graphics.DrawImage(icon, e.RowBounds.Left   2, e.RowBounds.Top   3, 16, 16);
            }
        }

        private void dataGridView1_Scroll(object sender, ScrollEventArgs e)
        {
            if (dataGridView1.HorizontalScrollingOffset <= 20)
                dataGridView1.HorizontalScrollingOffset = 0;
        }

    }       
}

实例下载地址

C# 域内打印机搜索安装 示例源码

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警