T
trint
How can I add all the network printers to a combobox?
Thanks,
Trint
Thanks,
Trint
trint said:How can I add all the network printers to a combobox?
Thanks,
Trint
trint said:How can I add all the network printers to a combobox?
Thanks,
Trint
This can easely be done when running in a domain where all printers are
registered in the AD, using System.DirectoryServices namespace classes.
It's much harder when running in a workgroup, you could use
System.Management classes and query each individual server in the network
for the shared printers. Of course this doesn't mean you can use these
printers as access privileges could prevent this.
If only thing you need is a list of printers visible from a specific
location, use System.Management and query the local visible printers.
here's a small sample to give you an idea...
using System;
using System.Management;
class App {
public static void Main() {
using(ManagementClass printerClass = new ManagementClass("win32_printer"))
{
ManagementObjectCollection printers = printerClass.GetInstances();
foreach(ManagementObject printer in printers)
{
Console.WriteLine("{0}",printer["Name"]);
if ((bool)printer["Shared"] == true)
Console.WriteLine("------> Shared as: {0}",printer["ShareName"]);
}
}
}
}
Willy.
trint said:Willy,
Thanks...can I use this same code to get the computers on the local
network also?
Thanks,
Trint
trint said:Willy,
Thanks...can I use this same code to get the computers on the local
network also?
Thanks,
Trint
No, obtaining the computers in a network is only possible when there is a
central authority where they are registered like a Domain Controller or the
AD.
Another, less prefered method is to PInvoke NetServerEnum .... like this:
using System;
using System.Runtime.InteropServices;
using System.Security;
sealed class Tester
{
[DllImport("Netapi32", CharSet=CharSet.Auto, SetLastError=true),
SuppressUnmanagedCodeSecurityAttribute]
static extern int NetServerEnum(
string ServerNane, // must be null
int dwLevel,
ref IntPtr pBuf,
int dwPrefMaxLen,
out int dwEntriesRead,
out int dwTotalEntries,
int dwServerType,
string domain, // null for login domain
out int dwResumeHandle
);
[DllImport("Netapi32", SetLastError=true),
SuppressUnmanagedCodeSecurityAttribute]
static extern int NetApiBufferFree(
IntPtr pBuf);
[StructLayout(LayoutKind.Sequential)]
struct _SERVER_INFO_100
{
internal int sv100_platform_id;
[MarshalAs(UnmanagedType.LPWStr)]
internal string sv100_name;
}
static void Main()
{
const int MAX_PREFERRED_LENGTH = -1;
int SV_TYPE_WORKSTATION = 1;
int SV_TYPE_SERVER = 2;
IntPtr buffer = IntPtr.Zero;
IntPtr tmpBuffer = IntPtr.Zero;
int entriesRead = 0;
int totalEntries = 0;
int resHandle = 0;
int sizeofINFO = Marshal.SizeOf(typeof(_SERVER_INFO_100));
try{
int ret = NetServerEnum(null, 100, ref buffer, MAX_PREFERRED_LENGTH,
out entriesRead,
out totalEntries, SV_TYPE_WORKSTATION|SV_TYPE_SERVER, null, out
resHandle);
if (ret == 0)
{
for (int i = 0; i < totalEntries ; i++)
{
tmpBuffer = new IntPtr((int)buffer + (i * sizeofINFO));
_SERVER_INFO_100 svrInfo = (_SERVER_INFO_100)
Marshal.PtrToStructure(tmpBuffer, typeof(_SERVER_INFO_100));
Console.WriteLine(svrInfo.sv100_name);
}
}
}
finally
{
NetApiBufferFree(buffer);
}
}
}
trint said:Willy,
I tried using this code and it keeps erroring out with:
An unhandled exception of type 'System.ComponentModel.Win32Exception'
occurred in system.windows.forms.dll
Thanks,
Trint
trint said:Willy,
Thanks...can I use this same code to get the computers on the local
network also?
Thanks,
Trint
No, obtaining the computers in a network is only possible when there is a
central authority where they are registered like a Domain Controller or the
AD.
Another, less prefered method is to PInvoke NetServerEnum .... like this:
using System;
using System.Runtime.InteropServices;
using System.Security;
sealed class Tester
{
[DllImport("Netapi32", CharSet=CharSet.Auto, SetLastError=true),
SuppressUnmanagedCodeSecurityAttribute]
static extern int NetServerEnum(
string ServerNane, // must be null
int dwLevel,
ref IntPtr pBuf,
int dwPrefMaxLen,
out int dwEntriesRead,
out int dwTotalEntries,
int dwServerType,
string domain, // null for login domain
out int dwResumeHandle
);
[DllImport("Netapi32", SetLastError=true),
SuppressUnmanagedCodeSecurityAttribute]
static extern int NetApiBufferFree(
IntPtr pBuf);
[StructLayout(LayoutKind.Sequential)]
struct _SERVER_INFO_100
{
internal int sv100_platform_id;
[MarshalAs(UnmanagedType.LPWStr)]
internal string sv100_name;
}
static void Main()
{
const int MAX_PREFERRED_LENGTH = -1;
int SV_TYPE_WORKSTATION = 1;
int SV_TYPE_SERVER = 2;
IntPtr buffer = IntPtr.Zero;
IntPtr tmpBuffer = IntPtr.Zero;
int entriesRead = 0;
int totalEntries = 0;
int resHandle = 0;
int sizeofINFO = Marshal.SizeOf(typeof(_SERVER_INFO_100));
try{
int ret = NetServerEnum(null, 100, ref buffer, MAX_PREFERRED_LENGTH,
out entriesRead,
out totalEntries, SV_TYPE_WORKSTATION|SV_TYPE_SERVER, null, out
resHandle);
if (ret == 0)
{
for (int i = 0; i < totalEntries ; i++)
{
tmpBuffer = new IntPtr((int)buffer + (i * sizeofINFO));
_SERVER_INFO_100 svrInfo = (_SERVER_INFO_100)
Marshal.PtrToStructure(tmpBuffer, typeof(_SERVER_INFO_100));
Console.WriteLine(svrInfo.sv100_name);
}
}
}
finally
{
NetApiBufferFree(buffer);
}
}
}
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.