打印机共享状态
/// <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;
}
}
例子文章来源:https://uudwc.com/A/4Poky
var printerShareState = ComputerInfo.GetPrinterShareState("TSC TE344", "TSC TE344");
文章来源地址https://uudwc.com/A/4Poky