本文介绍了打印问题(还有pageSettings,pagePreview)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在打印时遇到问题。我已经编写了一个程序,当我在Visual Studio中运行时,它可以完美运行。调用打印,页面设置和 pagePreview的按钮可以毫无问题地完成所有操作。但是,当我从发布文件夹(内置发布后)运行程序,然后单击按钮时,收到错误消息:您的应用程序中出现未处理的异常。如果您单击继续...,等等...

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...

例外文本

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?

好,我写了一个小程序,在这里我叫printPreviewDialog和printDialog。打印由printDocument完成。因此,我有一个带有两个按钮的表单。

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();
    }

当我在VS中运行此代码时-一切正常,但是当我运行时如果它从Release或Debug文件夹中返回,则程序将返回异常:未安装打印机。我已经尝试过两台计算机和一台虚拟机,但是到处都可以得到相同的结果,但有例外。

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.

推荐答案

在ASP.NET应用程序中,您必须是运行ASP.NET Web服务器的计算机上的管理员,或者如果您的用户帐户下运行的是ASP.NET辅助进程,则您的名称必须在Debugger Users组中。 ASP.NET应用程序默认情况下以NetworkService凭据运行

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

并且打印机通常安装在用户配置文件中(特别是如果是网络打印机,而不是物理连接的打印机,例如USB或并行电缆)。当您以管理员身份登录时看到的内容不一定意味着当其他用户登录同一台计算机时它们将可用。由于ASP.NET应用程序以NetworkService的身份运行,因此Web应用程序不太可能看到那里的打印机。

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.

因此,为.NET应用程序池创建一个要运行的帐户,然后为用户提供连接到打印机或添加打印机的权限。

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.

对于Windows应用程序或普通.Net应用程序,当您在Visual Studio上运行时,它将以管理员权限运行,但是当您安装了可执行文件并尝试访问打印机,因为运行此应用程序的用户帐户可能没有该打印机的权限,所以可能会收到此错误

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独立应用程序

.Net Standalone Application


  1. 请检查是否设置了默认打印机。

  2. 检查是否要通过代码使用默认打印机或其他任何要使用的打印机,它应该具有必要的权限,以便当前登录的用户可以访问它。为此,转到打印机设置->选择打印机->右键单击它并打开打印机属性。在安全性选项卡中,检查与您一起运行您的应用程序的当前登录用户是否具有访问该打印机的权限。

  3. 如果它仍然不起作用,请尝试向该用户授予权限大家。它应该工作。在这种情况下,可能很容易找出安装后出了什么问题。

ASP.NET应用程序

ASP.NET Application


  1. 在这种情况下需要澄清的第一件事是您的代码在服务器上执行,而不是在本地计算机上执行。您无法通过ASP.NET控制用户计算机上的打印。因此,请检查服务器上是否安装了打印机,并且其中一台打印机被标记为默认打印机。就像在调试模式下在本地环境中运行代码时,可能是在计算机上安装了打印机的情况,但是当您将应用程序托管在其他服务器上时,该服务器上没有安装打印机。

  2. 如果是这样,然后尝试将打印机设置为默认打印机

  3. 为此,请转到打印机设置->选择打印机->右键单击它并打开打印机属性。在安全性选项卡中,检查网络服务用户是否有权通过此打印机进行打印,如果没有,则将相关权限授予网络服务。

请进行检查,我希望解释清楚,并能够为此提供必要的帮助。

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

这篇关于打印问题(还有pageSettings,pagePreview)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 01:07