How can I add all the network printers to a combobox?
Thanks,
Trint 7 9078
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.goo glegroups.com.. . How can I add all the network printers to a combobox? Thanks, Trint
"trint" <tr***********@ gmail.com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.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.Director yServices namespace classes.
It's much harder when running in a workgroup, you could use
System.Manageme nt 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.Manageme nt and query the local visible printers.
here's a small sample to give you an idea...
using System;
using System.Manageme nt;
class App {
public static void Main() {
using(Managemen tClass printerClass = new ManagementClass ("win32_printer "))
{
ManagementObjec tCollection printers = printerClass.Ge tInstances();
foreach(Managem entObject printer in printers)
{
Console.WriteLi ne("{0}",printe r["Name"]);
if ((bool)printer["Shared"] == true)
Console.WriteLi ne("------> Shared as: {0}",printer["ShareName"]);
}
}
}
}
Willy.
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.goo glegroups.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.Director yServices namespace
classes. It's much harder when running in a workgroup, you could use System.Manageme nt 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.Manageme nt and query the local visible printers.
here's a small sample to give you an idea...
using System; using System.Manageme nt;
class App { public static void Main() {
using(Managemen tClass printerClass = new
ManagementClass ("win32_printer ")) { ManagementObjec tCollection printers = printerClass.Ge tInstances(); foreach(Managem entObject printer in printers) { Console.WriteLi ne("{0}",printe r["Name"]); if ((bool)printer["Shared"] == true) Console.WriteLi ne("------> Shared as:
{0}",printer["ShareName"]); } } } } Willy.
"trint" <tr***********@ gmail.com> wrote in message
news:11******** **************@ l41g2000cwc.goo glegroups.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("Neta pi32", CharSet=CharSet .Auto, SetLastError=tr ue),
SuppressUnmanag edCodeSecurityA ttribute]
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("Neta pi32", SetLastError=tr ue),
SuppressUnmanag edCodeSecurityA ttribute]
static extern int NetApiBufferFre e(
IntPtr pBuf);
[StructLayout(La youtKind.Sequen tial)]
struct _SERVER_INFO_10 0
{
internal int sv100_platform_ id;
[MarshalAs(Unman agedType.LPWStr )]
internal string sv100_name;
}
static void Main()
{
const int MAX_PREFERRED_L ENGTH = -1;
int SV_TYPE_WORKSTA TION = 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(n ull, 100, ref buffer, MAX_PREFERRED_L ENGTH,
out entriesRead,
out totalEntries, SV_TYPE_WORKSTA TION|SV_TYPE_SE RVER, null, out
resHandle);
if (ret == 0)
{
for (int i = 0; i < totalEntries ; i++)
{
tmpBuffer = new IntPtr((int)buf fer + (i * sizeofINFO));
_SERVER_INFO_10 0 svrInfo = (_SERVER_INFO_1 00)
Marshal.PtrToSt ructure(tmpBuff er, typeof(_SERVER_ INFO_100));
Console.WriteLi ne(svrInfo.sv10 0_name);
}
}
}
finally
{
NetApiBufferFre e(buffer);
}
}
}
Willy,
I tried using this code and it keeps erroring out with:
An unhandled exception of type 'System.Compone ntModel.Win32Ex ception'
occurred in system.windows. forms.dll
Thanks,
Trint
Willy Denoyette [MVP] wrote: "trint" <tr***********@ gmail.com> wrote in message news:11******** **************@ l41g2000cwc.goo glegroups.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("Neta pi32", CharSet=CharSet .Auto, SetLastError=tr ue), SuppressUnmanag edCodeSecurityA ttribute] 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("Neta pi32", SetLastError=tr ue), SuppressUnmanag edCodeSecurityA ttribute] static extern int NetApiBufferFre e( IntPtr pBuf);
[StructLayout(La youtKind.Sequen tial)] struct _SERVER_INFO_10 0 { internal int sv100_platform_ id; [MarshalAs(Unman agedType.LPWStr )] internal string sv100_name; } static void Main() { const int MAX_PREFERRED_L ENGTH = -1; int SV_TYPE_WORKSTA TION = 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(n ull, 100, ref buffer,
MAX_PREFERRED_L ENGTH, out entriesRead, out totalEntries, SV_TYPE_WORKSTA TION|SV_TYPE_SE RVER, null,
out resHandle); if (ret == 0) { for (int i = 0; i < totalEntries ; i++) { tmpBuffer = new IntPtr((int)buf fer + (i * sizeofINFO)); _SERVER_INFO_10 0 svrInfo = (_SERVER_INFO_1 00) Marshal.PtrToSt ructure(tmpBuff er, typeof(_SERVER_ INFO_100)); Console.WriteLi ne(svrInfo.sv10 0_name); } } } finally { NetApiBufferFre e(buffer); } } }
They are all registered under 'company.com'.. .by the way.
"trint" <tr***********@ gmail.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. . Willy, I tried using this code and it keeps erroring out with: An unhandled exception of type 'System.Compone ntModel.Win32Ex ception' occurred in system.windows. forms.dll Thanks, Trint
Willy Denoyette [MVP] wrote: "trint" <tr***********@ gmail.com> wrote in message news:11******** **************@ l41g2000cwc.goo glegroups.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("Neta pi32", CharSet=CharSet .Auto, SetLastError=tr ue), SuppressUnmanag edCodeSecurityA ttribute] 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("Neta pi32", SetLastError=tr ue), SuppressUnmanag edCodeSecurityA ttribute] static extern int NetApiBufferFre e( IntPtr pBuf);
[StructLayout(La youtKind.Sequen tial)] struct _SERVER_INFO_10 0 { internal int sv100_platform_ id; [MarshalAs(Unman agedType.LPWStr )] internal string sv100_name; } static void Main() { const int MAX_PREFERRED_L ENGTH = -1; int SV_TYPE_WORKSTA TION = 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(n ull, 100, ref buffer,
MAX_PREFERRED_L ENGTH, out entriesRead, out totalEntries, SV_TYPE_WORKSTA TION|SV_TYPE_SE RVER, null, out resHandle); if (ret == 0) { for (int i = 0; i < totalEntries ; i++) { tmpBuffer = new IntPtr((int)buf fer + (i * sizeofINFO)); _SERVER_INFO_10 0 svrInfo = (_SERVER_INFO_1 00) Marshal.PtrToSt ructure(tmpBuff er, typeof(_SERVER_ INFO_100)); Console.WriteLi ne(svrInfo.sv10 0_name); } } } finally { NetApiBufferFre e(buffer); } } }
What code? I didn't use Windows Forms in the sample, and you get an
Exception in system.windows. forms.dll.
Willy. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 example code or hand-holding on the web integration portion.
Anyone?
"Aaron_TekRecycle.com" <aaron_NAME@DOMAIN_tekrecycle.com> wrote in message
|
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 printers in my printers & faxes. However, when I use the WMI to
retrieve the printers in my system, it only retrieve 13 printers instead of
16.
|
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?
|
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 on the local machine which works fine when using a test windows application on the server and calling the function directly.
However if the test application first calls a Web Service which then calls the function to retrieve a list of prints I...
|
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 server machine locally. Anybody knows how
to do it?
Thanks,
Peng
| |
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 into server as a domain user that is in the local Administrators
group on the server
Workstation is on the same domain
Logged into the workstation as a domain user, which is also in the local
Administrators group on the server and workstation
|
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 get printer info.
problem here i m facing is , i m getting the printer info and am able to write the complete document in that case it is working fine..but while generation of document if it gets fail due to some runtime error, in the immediate run...
|
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 (printer properties). Usually when
what you're describing happens, it's because the printer driver is
substituting fonts that aren't built into the printer.
It's more efficient for the driver to use font substitution, because then
|
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 = "\\\\" + txtServerName.Text;
// Use the ObjectQuery to get the list of configured printers
System.Management.ObjectQuery oquery =
new System.Management.ObjectQuery("SELECT * FROM
Win32_Printer");
ConnectionOptions options = new ConnectionOptions();
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |