473,385 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Find shared printers??

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

Nov 17 '05 #1
7 9023
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
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
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
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
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
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
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
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
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
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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...

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.