Click here to Skip to main content
15,949,686 members
Home / Discussions / C#
   

C#

 
AnswerRe: Export File Pin
duncanmhor13-Nov-07 2:56
duncanmhor13-Nov-07 2:56 
AnswerRe: Export File Pin
SVb.net13-Nov-07 5:51
SVb.net13-Nov-07 5:51 
QuestionHow to control forecolor of a disabled label Pin
baelzaden13-Nov-07 1:39
baelzaden13-Nov-07 1:39 
Questionfile rename application Pin
Sunil Wise13-Nov-07 1:18
professionalSunil Wise13-Nov-07 1:18 
Questionadd column to datagridview Pin
arkiboys13-Nov-07 1:15
arkiboys13-Nov-07 1:15 
AnswerRe: add column to datagridview Pin
arkiboys13-Nov-07 2:40
arkiboys13-Nov-07 2:40 
QuestionDynamically Binding Data to a Label Pin
sindhutiwari13-Nov-07 1:14
sindhutiwari13-Nov-07 1:14 
QuestionAddPrinterConnection failing to add network printer Pin
ekynox13-Nov-07 0:52
ekynox13-Nov-07 0:52 
Hi guys,

have been playing with WMI to add a network printer connection to a Windows XP pc. My environment consists of a server running Windows Server 2003 and Visual Studio 2005 and a test pc running windows xp. Further, I have setup a full domain controller environment.

I have managed to write the code to add the network printer and it works fine when installing the printer on the same machine the code is executing from i.e the server or xp pc. However, if I run the code from the server to install the network printer onto the test pc, it does not work. An exception is generated stating "Not Supported, the inner exception is blank. I can manually install the network printer on the test pc via the control panel, proving that the connectivity between machines is fine. The exception occurs when the mgtClass.InvokeMethod is executed.

1) If I specifiy the ip address of the test pc in the ManagementScope object does and when an object of Management class is instantiated does this imply that the network printer will be added to the test pc?

2)Using the AddPrinterConnection method from the WIN32_PRINTER class, is this the only way to add a network printer ?

3) To install the network printer on the server itself, omit the IP address,ipAddressOfClientPC, of the server from the ManagementScope object and it installs fine, provided the code itself is running of the server. So this leads me to believe its an issue with ManagementScope. However, if installing on the same machine as the code is running on then the username and password need not to be defined. So what do I need to do to the Management Scope object to make it work?

I used the WMI code generated by the WMI Code Generator, http://www.microsoft.com/downloads/details.aspx?FamilyID=2cc30a64-ea15-4661-8da4-55bbc145c30e,

I have added my code below.

If somone can point me in the right direction as to the mistake I am making, I would appreciate it.

thanks
V
<br />
using System;<br />
using System.Management;<br />
using System.Runtime.InteropServices;<br />
using System.Text;<br />
<br />
namespace SampleApp<br />
{<br />
    public class AddPrinterConnection<br />
    {<br />
        public static void Main()<br />
        {<br />
            //only required if required to access another machine with different username and password<br />
            string username = "administrator";<br />
            string password = "password";<br />
<br />
            string ipAddressOfClientPC = "192.168.0.22";<br />
            ManagementScope scope = new ManagementScope("\\\\" + ipAddressOfClientPC + "\\root\\cimv2");<br />
<br />
            <br />
            scope.Options.Username = username;<br />
            scope.Options.Password = password;<br />
            scope.Options.Impersonation = ImpersonationLevel.Impersonate;<br />
<br />
            scope.Connect();<br />
<br />
            ManagementPath path = new ManagementPath("Win32_Printer");<br />
            ManagementClass mgtClass = new ManagementClass(scope, path, null);<br />
            using (mgtClass)<br />
            {<br />
                try<br />
                {<br />
                    ManagementBaseObject inputParameters = mgtClass.GetMethodParameters("AddPrinterConnection");<br />
<br />
                    string sharedPrinterAddress = "\\\\192.168.0.1\\printer2";<br />
                    inputParameters["Name"] = sharedPrinterAddress;<br />
<br />
                    ManagementBaseObject outputParameters = mgtClass.InvokeMethod("AddPrinterConnection", inputParameters, null);<br />
                    uint errorCode = (uint)outputParameters["ReturnValue"];<br />
<br />
                    Console.WriteLine(errorCode.ToString());<br />
                }<br />
                catch (Exception exception)<br />
                {<br />
                    Console.WriteLine(exception.Message + "\\n" + exception.InnerException);<br />
                }<br />
                Console.ReadLine();<br />
            }<br />
        }<br />
    }<br />
}<br />
<br />

AnswerRe: AddPrinterConnection failing to add network printer Pin
ekynox15-Nov-07 9:44
ekynox15-Nov-07 9:44 
QuestionDynamic controls in web Pin
M. J. Jaya Chitra13-Nov-07 0:45
M. J. Jaya Chitra13-Nov-07 0:45 
Questionvs2005, I need to connect data source using c# Pin
kkooring13-Nov-07 0:28
kkooring13-Nov-07 0:28 
QuestionXMLDocument.GetElementsByTagName returns comments ! Pin
N a v a n e e t h13-Nov-07 0:16
N a v a n e e t h13-Nov-07 0:16 
AnswerRe: XMLDocument.GetElementsByTagName returns comments ! Pin
Sun Rays13-Nov-07 0:27
Sun Rays13-Nov-07 0:27 
QuestionDoes C# provide all the functionalities of WinPcap? Pin
Diana Fernandez12-Nov-07 23:37
Diana Fernandez12-Nov-07 23:37 
QuestionSetting Print Job Parameters Pin
edgar_raj12-Nov-07 23:33
edgar_raj12-Nov-07 23:33 
AnswerRe: Setting Print Job Parameters Pin
Vasudevan Deepak Kumar13-Nov-07 20:34
Vasudevan Deepak Kumar13-Nov-07 20:34 
QuestionCustom Control in C# Pin
Abhijit Jana12-Nov-07 22:52
professionalAbhijit Jana12-Nov-07 22:52 
Questionpassword assgning application? Pin
shaz jazz12-Nov-07 22:20
shaz jazz12-Nov-07 22:20 
AnswerRe: password assgning application? Pin
Christian Graus12-Nov-07 22:31
protectorChristian Graus12-Nov-07 22:31 
AnswerRe: password assgning application? Pin
Sun Rays12-Nov-07 22:33
Sun Rays12-Nov-07 22:33 
GeneralRe: password assgning application? Pin
shaz jazz13-Nov-07 2:32
shaz jazz13-Nov-07 2:32 
GeneralRe: password assgning application? Pin
shaz jazz22-Nov-07 11:42
shaz jazz22-Nov-07 11:42 
AnswerRe: password assgning application? Pin
Michael Sync12-Nov-07 22:37
Michael Sync12-Nov-07 22:37 
AnswerRe: password assgning application? Pin
Adeel Chaudhry12-Nov-07 23:27
Adeel Chaudhry12-Nov-07 23:27 
QuestionDynamic Table Alignment from C#.Net codebehind Pin
jaishankar270412-Nov-07 21:56
jaishankar270412-Nov-07 21:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.