111,092
社区成员




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);
}
}
<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>
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);
}
}
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);
}
}
}
}
}