【C#】判断打印机共享状态

打印机共享状态

    /// <summary>
    /// 打印机共享状态
    /// </summary>
    public enum PrinterShareState
    {
        /// <summary>
        /// 无打印机
        /// </summary>
        None = -1,
        /// <summary>
        /// 未共享
        /// </summary>
        NotShare = 0,
        /// <summary>
        /// 已共享
        /// </summary>
        Shared = 1,
        /// <summary>
        /// 已共享,但名称与预期不匹配
        /// </summary>
        ShareNameError = 2
    }

方法

    public partial class ComputerInfo
    {

        /// <summary>
        /// 判断打印机共享状态
        /// </summary>
        /// <param name="printerName"></param>
        /// <param name="shareName"></param>
        public static PrinterShareState GetPrinterShareState(string printerName, string shareName)
        {
            PrinterShareState printerShareState = PrinterShareState.None;
            ManagementClass mc = new ManagementClass("Win32_Printer");
            ManagementObjectCollection moc = mc.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                var deviceID = mo["DeviceID"].ToString();
                if (deviceID == printerName)
                {
                    var shared = (bool)mo["Shared"];
                    if (shared)
                    {
                        var ShareName = mo["ShareName"].ToString();
                        if (ShareName == shareName)
                        {
                            printerShareState = PrinterShareState.Shared;
                        }
                        else
                        {
                            printerShareState = PrinterShareState.ShareNameError;
                        }
                    }
                    else
                    {
                        printerShareState = PrinterShareState.NotShare;
                    }
                    break;
                }
            }
            return printerShareState;
        }

    }

例子

var printerShareState = ComputerInfo.GetPrinterShareState("TSC TE344", "TSC TE344");

文章来源地址https://uudwc.com/A/4Poky

原文地址:https://blog.csdn.net/weixin_38211198/article/details/132249680

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请联系站长进行投诉反馈,一经查实,立即删除!

上一篇 2023年08月13日 08:43
并查集、树状数组
下一篇 2023年08月13日 08:44