Link to home
Start Free TrialLog in
Avatar of pauledwardian
pauledwardian

asked on

Add printer

Why on earth this code wouldnt work on a client computer but it will work on my computer IF I directly run it from visual studio?

    [DllImport("winspool.drv")]
        public static extern bool AddPrinterConnection(string pName);
        [DllImport("winspool.drv")]
        public static extern bool SetDefaultPrinter(string printerName);
        public void printers()
        {


            string printername = this.ListBox1.SelectedItem.Text.ToString();
            bool result = AddPrinterConnection(@"\\Servername\" + printername);
            bool resultdef = SetDefaultPrinter(@"\\Servername\" + printername);

        }

Open in new window


FYI: ASP.NET C#
Avatar of Randy Downs
Randy Downs
Flag of United States of America image

Maybe it's a permissions issue. Try running on client as admin.
Avatar of pauledwardian
pauledwardian

ASKER

Yes, the client is a local admin on the PC.
kalpesh2804,
Is there any otherway to do it without going trhough WMI? How can I manage using my code to get it to work?
Maybe this will help

http://stackoverflow.com/questions/818583/how-can-i-install-a-printer-using-net
      

I believe this is possible via interop to native win32 APIs, but I've found its much, much easier just to use a System.Diagnostics.Process() to call into printui.dll via:

rundll32.exe printui.dll,PrintUIEntry /?
I tried that too but I still cannot do it from the client side on the website.
Can the user add a printer through the wizard?
yes
Does your code work if you try it via the web on your machine? Same O/S as the client that fails?
Yes but only from visual studio
ASKER CERTIFIED SOLUTION
Avatar of Randy Downs
Randy Downs
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks....