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#
C#ASP.NET.NET Programming

Avatar of undefined
Last Comment
pauledwardian

8/22/2022 - Mon
Randy Downs

Maybe it's a permissions issue. Try running on client as admin.
Kalpesh Chhatrala

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?
Your help has saved me hundreds of hours of internet surfing.
fblack61
Randy Downs

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 /?
pauledwardian

ASKER
I tried that too but I still cannot do it from the client side on the website.
Randy Downs

Can the user add a printer through the wizard?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
pauledwardian

ASKER
yes
Randy Downs

Does your code work if you try it via the web on your machine? Same O/S as the client that fails?
pauledwardian

ASKER
Yes but only from visual studio
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ASKER CERTIFIED SOLUTION
Randy Downs

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
pauledwardian

ASKER
Thanks....