problems with print(also pageSettings, pagePreview

2019-08-30 02:49发布

I have some problem with printing. I have written a program, which when I run inside Visual Studio works perfect. The buttons which call Print, PageSettings and pagePreview do everything without any problem. But, when I run my program from Release Folder (after Built Release) and then click on buttons I receive Wrong messsage: Unhandled exception has occured in your application. If you click continue... etc...

Exception Text

System.Drawing.Printing.InvalidPrinterException: No printers are installed.

It says that no printers are installed, but it isn't true. And why does it works under VS?

Ok, I wrote a little program, where I call printPreviewDialog and printDialog. Print is being done by printDocument. So, I have a form with two buttons.

    private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        e.Graphics.DrawString("Hello World", new Font("Times New Roman", 16, FontStyle.Bold), Brushes.Black, 10, 10);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        printPreviewDialog1.Document = printDocument1;
        printPreviewDialog1.ShowDialog();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        printDialog1.Document = printDocument1;
        printDialog1.ShowDialog();
    }

When I run this code in VS - everything works perfect, but when I run it from Release or Debug folder the program returns exception: No printers are installed. I have already tried into two computers and one virtual machine, but everywhere I got the same result with exception.

1条回答
We Are One
2楼-- · 2019-08-30 03:32

To debug an ASP.NET application, you must be an administrator on the machine that the ASP.NET Web server is running or your name must be in the Debugger Users group if the ASP.NET worker process is running under your user account. Whereas ASP.NET applications runs as NetworkService credential by default

And Printers are usually installed on user's profile (especially if it's a network printer, not a physically connected one such as on USB or parallel cable). What you see when logging in as Administrator doesn't necessarily mean they'll be available when other user is logging on the same machine. As ASP.NET applications runs as NetworkService so the web application is unlikely to see the printers there.

So create an account for .NET application pool to run on, and then provide permissions for that user to connect to the printers or Add Printers.

For Windows application or normal .Net Application when you run on Visual Studio it runs with Administrator rights but when you install the executable and try to access the printer you may get this error because the user account with which you are running this application may not have rights to that printer

So I suggest to got through all of these mentioned points step by step

.Net Standalone Application

  1. Please check that default Printer is set.
  2. Check if the Default Printer or any other Printer you want to use via code, it should have necessary permissions so that the current logged in user can access it. For this go on to Printer Settings -> Select the Printer -> right click on it and open Printer properties. In the security tab check if the current logged in user with which you are running your application with have rights to access that printer.
  3. If it is still not working try giving permissions to the user "Everyone". It should work. May be it will be easy to figure out in this case what goes wrong after installation.

ASP.NET Application

  1. First thing which needs to be clarified in this case is that your code executes on the server, not the local machine. You cannot control printing on users machines through ASP.NET. So please check if the Printer is installed on the server and one of the printer is marked as default printer. As when you run your code in local environment in debug mode it may be a case that printer is installed on your machine but when you hosted your application on some other server then there is no printer installed on that server.
  2. If so, then try setting a printer as a default printer
  3. For this go on to Printer Settings -> Select the Printer -> right click on it and open Printer properties. In the security tab check if the user "Network Service" have access to print through this printer, if not then give the relevant permissions to the "Network Service".

Please make these checks, i hope that the explanation is clear and i am able to provide you the necessary help through this.

查看更多
登录 后发表回答