常用代码

Window打印机

2021-05-11  本文已影响0人  RICK_216
namespace Sp.Base.Core.Utils
{
    public class WindowsUtil
    {
        #region 设备

        /// <summary>
        /// 打印机
        /// </summary>
        /// <returns></returns>
        public static string[] GetPrinters()
        {
            string[] installedPrinters = new string[PrinterSettings.InstalledPrinters.Count];
            PrinterSettings.InstalledPrinters.CopyTo(installedPrinters, 0);
            return installedPrinters;
        }

        public static PrinterStatus GetPrinterStatus(string printerName)
        {
            string path = $"win32_printer.DeviceId='{printerName}'";
            ManagementObject printer = new ManagementObject(path);
            printer.Get();
            PrinterStatus ret = (PrinterStatus)Convert.ToInt32(printer.Properties["PrinterStatus"].Value);
            return ret;
        }
        #endregion
    }

    #region 枚举
    /// <summary>
    /// 打印状态
    /// </summary>
    public enum PrinterStatus
    {
        /// <summary>
        /// 其他状态
        /// </summary>
        Other = 1,
        /// <summary>
        /// 未知
        /// </summary>
        UnKnow,
        /// <summary>
        /// 空闲
        /// </summary>
        Waiting,
        /// <summary>
        ///  正在打印
        /// </summary>
        Printing,
        /// <summary>
        /// 预热
        /// </summary>
        WarmingUp,
        /// <summary>
        /// 停止打印
        /// </summary>
        Paused,
        /// <summary>
        ///  打印中
        /// </summary>
        Processing,
        /// <summary>
        /// 离线
        /// </summary>
        Offline
    }
    #endregion
}

上一篇下一篇

猜你喜欢

热点阅读