用C# WinForms如何检测整个局域网内的所有打印机是否可用.

zjhappy520 2008-08-28 03:03:29
各位帮帮忙啊.. 补充一下:就是本机不添加打印机的情况下检测...
...全文
530 30 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
30 条回复
切换为时间正序
请发表友善的回复…
发表回复
hmhhmhhmh 2011-08-14
  • 打赏
  • 举报
回复


private void button5_Click(object sender, EventArgs e)
{
DirectoryEntry printer = new DirectoryEntry("WinNT://servername/printqueuename", null, null, AuthenticationTypes.ServerBind);
listBox1.Items.Add(printer.Path);
System.DirectoryServices.PropertyCollection pcoll = printer.Properties;
try
{
foreach(string sc in pcoll.PropertyNames)
{
listBox1.Items.Add(sc + ":\t" + (pcoll[sc])[0].ToString());
}
listBox1.Items.Add("---------------------------------");
}
catch(COMException ex)
{
listBox1.Items.Add(ex.Message);
}
}



红色改为//104print(电脑名字)/EPSON Stylus Photo R230 Series(打印机名字)


运行结果如下:

<select size="4" name="ListBox1" id="ListBox1" style="height:343px;width:481px;">
<option value="WinNT://104print/EPSON Stylus Photo R230 Series">WinNT://104print/EPSON Stylus Photo R230 Series</option>
<option value="PrinterName: \\104print\EPSON Stylus Photo R230 Series">PrinterName: \\104print\EPSON Stylus Photo R230 Series</option>
<option value="Model: EPSON Stylus Photo R230 Series">Model: EPSON Stylus Photo R230 Series</option>
<option value="PrintProcessor: WinPrint">PrintProcessor: WinPrint</option>
<option value="Location: ">Location: </option>
<option value="Datatype: RAW">Datatype: RAW</option>
<option value="BannerPage: ">BannerPage: </option>
<option value="Description: ">Description: </option>
<option value="PrinterPath: \\104print\EPSON Stylus Photo R230 Series">PrinterPath: \\104print\EPSON Stylus Photo R230 Series</option>
<option value="Priority: 1">Priority: 1</option>
<option value="PrintDevices: USB001">PrintDevices: USB001</option>
<option value="DefaultJobPriority: 0">DefaultJobPriority: 0</option>
<option value="JobCount: 0">JobCount: 0</option>
<option value="Attributes: 2584">Attributes: 2584</option>
<option value="StartTime: 1899-12-30 8:00:00">StartTime: 1899-12-30 8:00:00</option>
<option value="UntilTime: 1899-12-30 8:00:00">UntilTime: 1899-12-30 8:00:00</option>
<option value="Name: EPSON Stylus Photo R230 Series">Name: EPSON Stylus Photo R230 Series</option>
<option value="ObjectGUID: ">ObjectGUID: </option>
<option value="Action: 4">Action: 4</option>
<option value="---------------------------------">---------------------------------</option>

</select>


作用列单台打印机的信息。
C771048104 2011-03-07
  • 打赏
  • 举报
回复
怎么引用24楼的代码
C771048104 2011-03-07
  • 打赏
  • 举报
回复
OK~~~
DoomGuardian 2010-07-01
  • 打赏
  • 举报
回复
怎么看不了啊
zjhappy520 2008-09-02
  • 打赏
  • 举报
回复
24楼,太谢谢你了, 不过你也来得太晚了点啊. 早点来那就爱死你了.
zt_100094 2008-08-29
  • 打赏
  • 举报
回复
private PrinterStatus GetPrinterStat(string PrinterDevice)
{
PrinterStatus ret = 0;
string path = @"win32_printer.DeviceId='" + PrinterDevice + "'";
ManagementObject printer = new ManagementObject(path);
printer.Get();
ret = (PrinterStatus)Convert.ToInt32(printer.Properties["PrinterStatus"].Value);
return ret;
}



调用:
private void 打印预览ToolStripMenuItem_Click(object sender, EventArgs e)
{
foreach (string strPrinter in PrinterSettings.InstalledPrinters)
{
if (printDocument1.PrinterSettings.PrinterName == null)
{
MessageBox.Show("没有检测到已安装的打印机!", "警示");
}
else
{
printDocument1.PrinterSettings.PrinterName = strPrinter;
PrinterStatus nn = new PrinterStatus();
nn = GetPrinterStat(printDocument1.PrinterSettings.PrinterName);
if (nn.ToString() == "空闲")
{
try
{
if (InitializePrinting())
{
printPreviewDialog1.ShowDialog();
}

}
catch //(Exception excep)
{

MessageBox.Show("无法访问打印机!", "打印出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
printDocument1.PrintController.OnEndPrint(printDocument1, new PrintEventArgs());
}

return;
}
}
}
}
zjhappy520 2008-08-29
  • 打赏
  • 举报
回复
哎,楼上的代码,我都早就试过了.网上的资料,我可能是找完了吧..
顶啊,,帮帮大家帮帮忙啊.. 搞定了马上加分,结帖.
slimfeng 2008-08-29
  • 打赏
  • 举报
回复

private void button4_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
foreach(String Name in System.Drawing.Printing.PrinterSettings.InstalledPrinters)
{
listBox1.Items.Add(Name);
}

}
//下面是据说可以找到所有,但我测试了一下不行,不太清楚"WinNT://servername/printqueuename"中servername以及printqueuename怎么设置

private void button5_Click(object sender, EventArgs e)
{
DirectoryEntry printer = new DirectoryEntry("WinNT://servername/printqueuename", null, null, AuthenticationTypes.ServerBind);
listBox1.Items.Add(printer.Path);
System.DirectoryServices.PropertyCollection pcoll = printer.Properties;
try
{
foreach(string sc in pcoll.PropertyNames)
{
listBox1.Items.Add(sc + ":\t" + (pcoll[sc])[0].ToString());
}
listBox1.Items.Add("---------------------------------");
}
catch(COMException ex)
{
listBox1.Items.Add(ex.Message);
}
}

zjhappy520 2008-08-29
  • 打赏
  • 举报
回复
我这里不可以上QQ啊..郁闷死了. 大家帮帮忙啊, 昨天搞了半天, 急啊, 要不没饭碗了啊...
欧文 2008-08-29
  • 打赏
  • 举报
回复
是啊,全是电脑名字啊
hm7921936 2008-08-29
  • 打赏
  • 举报
回复
mk
zjhappy520 2008-08-29
  • 打赏
  • 举报
回复
4楼,你的代码怎么是把所有电脑名都查出来了.
chenyijiu 2008-08-29
  • 打赏
  • 举报
回复
贴段能用的,查找网络打印机



public class MyEnumPrintersClass
{
[FlagsAttribute]
public enum PrinterEnumFlags
{
PRINTER_ENUM_DEFAULT = 0x00000001,
PRINTER_ENUM_LOCAL = 0x00000002,
PRINTER_ENUM_CONNECTIONS = 0x00000004,
PRINTER_ENUM_FAVORITE = 0x00000004,
PRINTER_ENUM_NAME = 0x00000008,
PRINTER_ENUM_REMOTE = 0x00000010,
PRINTER_ENUM_SHARED = 0x00000020,
PRINTER_ENUM_NETWORK = 0x00000040,
PRINTER_ENUM_EXPAND = 0x00004000,
PRINTER_ENUM_CONTAINER = 0x00008000,
PRINTER_ENUM_ICONMASK = 0x00ff0000,
PRINTER_ENUM_ICON1 = 0x00010000,
PRINTER_ENUM_ICON2 = 0x00020000,
PRINTER_ENUM_ICON3 = 0x00040000,
PRINTER_ENUM_ICON4 = 0x00080000,
PRINTER_ENUM_ICON5 = 0x00100000,
PRINTER_ENUM_ICON6 = 0x00200000,
PRINTER_ENUM_ICON7 = 0x00400000,
PRINTER_ENUM_ICON8 = 0x00800000,
PRINTER_ENUM_HIDE = 0x01000000
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct PRINTER_INFO_1
{
int flags;
[MarshalAs(UnmanagedType.LPTStr)]
public string pDescription;
[MarshalAs(UnmanagedType.LPTStr)]
public string pName;
[MarshalAs(UnmanagedType.LPTStr)]
public string pComment;
}

[DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)]
static extern bool EnumPrinters(PrinterEnumFlags Flags, string Name, uint Level, IntPtr pPrinterEnum, uint cbBuf, ref uint pcbNeeded, ref uint pcReturned);

private const int ERROR_INSUFFICIENT_BUFFER = 122;

public static PRINTER_INFO_1[] MyEnumPrinters(PrinterEnumFlags Flags)
{
uint cbNeeded = 0;
uint cReturned = 0;
IntPtr pPrInfo4 = IntPtr.Zero;
uint size = 0;

if (EnumPrinters(Flags, null, 1, IntPtr.Zero, size, ref cbNeeded, ref cReturned))
{
return new PRINTER_INFO_1[]{};
}
if (cbNeeded != 0)
{
pPrInfo4 = Marshal.AllocHGlobal((int)cbNeeded + 128);
size = cbNeeded + 128;
EnumPrinters(Flags, null, 1, pPrInfo4, size, ref cbNeeded, ref cReturned);
if (cReturned != 0)
{
PRINTER_INFO_1[] printerInfo1 = new PRINTER_INFO_1[cReturned];
int offset = pPrInfo4.ToInt32();
Type type = typeof(PRINTER_INFO_1);
int increment = Marshal.SizeOf(type);
for (int i = 0; i < cReturned; i++)
{
printerInfo1[i] = (PRINTER_INFO_1)Marshal.PtrToStructure(new IntPtr(offset), type);
offset += increment;
}
Marshal.FreeHGlobal(pPrInfo4);
return printerInfo1;
}
}

return new PRINTER_INFO_1[]{};
}
public static void Main(String[] args)
{
PRINTER_INFO_1[] printers = MyEnumPrinters(PrinterEnumFlags.PRINTER_ENUM_REMOTE);
foreach (PRINTER_INFO_1 printer in printers)
{
if (-1 == printer.pName.IndexOf("!!"))
{
Console.WriteLine(printer.pName);
}
else
{
uint cbNeeded = 0;
uint cReturned = 0;
IntPtr pPrInfo4 = IntPtr.Zero;
uint size = 0;
string pNewName = printer.pName;
EnumPrinters(PrinterEnumFlags.PRINTER_ENUM_NAME, pNewName, 1, IntPtr.Zero, size, ref cbNeeded,ref cReturned);
if (cbNeeded != 0)
{
pPrInfo4 = Marshal.AllocHGlobal((int)cbNeeded + 128);
size = cbNeeded + 128;
EnumPrinters(PrinterEnumFlags.PRINTER_ENUM_NAME, pNewName, 1, pPrInfo4, size, ref cbNeeded, ref cReturned);
PRINTER_INFO_1[] printerInfo1 = new PRINTER_INFO_1[cReturned];
int offset = pPrInfo4.ToInt32();
Type type = typeof(PRINTER_INFO_1);
int increment = Marshal.SizeOf(type);
for (int i = 0; i < cReturned; i++)
{
printerInfo1[i] = (PRINTER_INFO_1)Marshal.PtrToStructure(new IntPtr(offset), type);
offset += increment;
Console.WriteLine(printerInfo1[i].pName);
}
Marshal.FreeHGlobal(pPrInfo4);
}
}
}
}
}
zjhappy520 2008-08-29
  • 打赏
  • 举报
回复
20楼, 为什么你说的我完全看不懂. SNMP源代码 在哪里可以下啊. 你不能帮人帮到底吗, 做个例子出来..谢谢了啊.
sxmonsy 2008-08-29
  • 打赏
  • 举报
回复
顶下楼上的
sxmonsy 2008-08-29
  • 打赏
  • 举报
回复
顶下楼上的
lextm 2008-08-29
  • 打赏
  • 举报
回复
#SNMP源代码里面自带的例子有一个TestDiscovery的,是用来查看当前局域网中支持SNMP设备用的例子。你可以看看你当前局域网内有哪些机器支持SNMP的。以我的网络为例,从显示出的名字还可以看出哪些是HP的打印机来。但是最好可以用打印机专有的对象来看,而不是简单的sysDesc。

最主要是看起来你对于TCP/IP,SNMP都不是太了解,我也不能打包票你一个钟头就搞定吧。
zjhappy520 2008-08-29
  • 打赏
  • 举报
回复
不明白, 有没有代码示例啊.. 谢谢了...帮帮忙啊今天又过去半天了..
lextm 2008-08-29
  • 打赏
  • 举报
回复
使用一点SNMP就可以。需要使用我开发的#SNMP。http://www.codeplex.com/sharpsnmplib

通过GetRequestMessage.Broadcast函数向255.255.255.255或者其他广播IP发送只有打印机支持的SNMP对象请求,就可以得出局域网内哪些设备是可用的打印机。不过这需要该类型的打印机支持SNMP。
zjhappy520 2008-08-29
  • 打赏
  • 举报
回复
楼上的还是不行啊. 这种方法只能访问到本机的.. 不是整个局域网的..
加载更多回复(9)

111,092

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧