473,509 Members | 2,900 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Find shared printers??

How can I add all the network printers to a combobox?
Thanks,
Trint

Nov 17 '05 #1
7 9051
Take a look at this link:

http://msdn.microsoft.com/library/de...sharecheck.asp

I am not sure if this is available in .NET but it can certainly be used via
P/Invoke.

"trint" <tr***********@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
How can I add all the network printers to a combobox?
Thanks,
Trint

Nov 17 '05 #2

"trint" <tr***********@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
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.
Nov 17 '05 #3
Willy,
Thanks...can I use this same code to get the computers on the local
network also?
Thanks,
Trint
Willy Denoyette [MVP] wrote:
"trint" <tr***********@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
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.


Nov 17 '05 #4

"trint" <tr***********@gmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
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);
}
}
}

Nov 17 '05 #5
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
Willy Denoyette [MVP] wrote:
"trint" <tr***********@gmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
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);
}
}
}


Nov 17 '05 #6
They are all registered under 'company.com'...by the way.

Nov 17 '05 #7

"trint" <tr***********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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
Willy Denoyette [MVP] wrote:
"trint" <tr***********@gmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
> 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);
}
}
}


What code? I didn't use Windows Forms in the sample, and you get an
Exception in system.windows.forms.dll.

Willy.
Nov 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
5885
by: Aaron_TekRecycle.com | last post by:
Someone must have done this before?!? I have VBS code that will Enumerate all the Printers in the AD and Add the Printer Connection to the client... I'm just not a web developer so I need some...
1
1474
by: Vanessa | last post by:
Hi, I'm trying to loop through all the printers in my computer system using WMI. However, I found out that it doesn't really get the correct number of printers in my system. I have 16...
1
2853
by: TheThrill | last post by:
I've got a report, Report1 that i want to print to network Printers A, B, C all with one key stroke. How do i do this?
5
20527
by: Bill Gates | last post by:
Hello, I am having a little trouble accessing a list of printers on our Network through a web service... I am using the PrinterSettings.InstalledPrinters to access a list of printers installed...
56
13967
by: peng | last post by:
Hi, I am development a project using C#.Net. Inside application, it needs to print labels on different Zebra label printers on the network. I used a shell script, but it only worked on the...
5
6671
by: lmttag | last post by:
ASP.NET 2.0 (C#) application Intranet application (not on the Internet) Using Windows authentication and impersonation Windows Server 2003 (IIS6) Server is a member server on a domain Logged...
0
1566
by: Ravigwipro | last post by:
Hi, I m able to get the printers object to know what are the printers had been installed and all. here my requirement is i have to write a data from VB to MS Word. for the page setup i have to...
0
1209
by: Peter Duniho | last post by:
On Wed, 23 Apr 2008 09:40:14 -0700, Al Meadows <fineware@fineware.com> wrote: This doesn't really seem to be a .NET or C# question. However, you may want to look at the driver settings...
0
1747
by: =?Utf-8?B?ZmFyc2hhZA==?= | last post by:
The following WMI code only retrieves the list of local shared printers on a target machine but NOT the list of shared REMOTE printers. Any suggestions please? string strWapiServer = "\\\\" +...
0
7237
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7347
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7416
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7073
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7506
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
3218
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3207
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
779
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
443
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.