简体   繁体   中英

C# console application exe file does not start

I have created a console application project at .NET framework 4.0 (I tried to change it several times). In order to have abilities to print images via this app, I have added a reference for the System.Drawing assembly. When I run that app in VS - it works fine, but once I run the exe file - it always shows in processes but does not do anything, moreover I cannot finish it's process.

using System;
using System.Drawing.Printing;


namespace printCA
{
    class Program
    {
        static void Main(string[] args)
        {

                string filePath;
                if (args.Length > 1)
                    filePath = args[1];
                else
                    filePath ="D:\\image.jpg";



            PrintDocument p = new PrintDocument();
            p.PrintPage += delegate (object sender1, PrintPageEventArgs e1)
            {
                System.Drawing.Image img = System.Drawing.Image.FromFile(filePath);
                System.Drawing.Point loc = new System.Drawing.Point(0, 0);
                e1.Graphics.DrawImage(img, loc);


            };
            try
            {
                p.Print();
            }
            catch (Exception ex)
            {
                throw new Exception("Exception Occured While Printing", ex);
            }
        }
    }
}

I suspect your EXE is exiting before your delegate has fired. You should insert some diagnostics and try putting this at the end to stop the process exiting:

Console.WriteLine("Press RETURN> ");
Console.ReadLine();

禁用防病毒软件后,该应用程序开始正常运行。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM