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.
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
ASP.NET Application
Please make these checks, i hope that the explanation is clear and i am able to provide you the necessary help through this.