Click here to Skip to main content
15,886,797 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
This is my code for printing this is working file on Visual studio.But when I am running this code on IIS 7.5 it is not running..



C#
[DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern bool SetDefaultPrinter(string Name);
    public string PrintContent { get; set; }
    

public void PrintReceipt(string path)
{
    try
    {

        PrinterSettings settings = new PrinterSettings();
        string currentDefualtPrinter = settings.PrinterName;
        UserBll obj = new UserBll();
        DataTable tb = obj.GetSystemInfo();
        if (tb != null && tb.Rows.Count > 0)
        {
            SetDefaultPrinter(Convert.ToString(tb.Rows[0]["ReceiptPrinter"]));
        }
        //   String st = @"E:\New Microsoft Word Document.docx";

        System.Diagnostics.Process process = new System.Diagnostics.Process();
        process.StartInfo.Verb = "Print";
        process.StartInfo.FileName = path;
        process.StartInfo.CreateNoWindow = true;
        process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        process.Start();
        process.WaitForExit();
        process.Close();
    }
    catch (Exception ex)
    {
    }
}
Posted
Updated 18-Jul-14 18:29pm
v4
Comments
Richard MacCutchan 16-Jul-14 7:49am    
You need to explain exactly what the circumstances are and what happens. Saying "not running" provides no information to help diagnose your problem.
[no name] 16-Jul-14 7:51am    
Is there a printer actually attached to your webserver? What is the point of having an empty catch block? And the user permission that you are running under probably does not have access rights.
Rohit from Delhi 19-Jul-14 0:32am    
It is throwing exception "Win32 exception : The specified executable is not a valid application for this OS platform."

I have printer attached to my PC and when I printing it locally it is printting . Error is when printing from IIS.
[no name] 19-Jul-14 7:38am    
And? "The specified executable is not a valid application for this OS platform" is pretty obvious is it not?

1 solution

Yes, it is working.
Or at least, it's trying to.

The problem is that C# code is executed on the Server, not the Client.
When you are running in your development system, that didn't matter - because the Client and the Server were the same computer, so it looked like this code was working.

Now you are running on the production server which is a completely separate PC and does not appear to be running on the same OS - which is what I would expect.

You can't do this. You cannot print on the Client printer by trying to print like that!
Have a look here: print client C#[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900