In C# 7.1, the language extends the valid signatures of an entrypoint to allow these async overloads of the Main method to be valid. public static void Main(); public static int Main(); public static void Main(string[] args); public static int Main(string[] args); public static Task Main(); public static Task<int> Main(); public static Task Main(string[] args); public static Task<int> Main(string[] args);
using System; using System.Diagnostics; using System.IO; using System.Management; using Microsoft.Win32; namespace PdfFilePrinter { /// <summary> /// Executes the Adobe Reader and prints a file while suppressing the Acrobat print /// dialog box, then terminating the Reader. /// </summary> public class AcroPrint { /// <summary> /// The Adobe Reader or Adobe Acrobat path such as 'C:\Program Files\Adobe\Adobe Reader X\AcroRd32.exe'. /// If it's not specified, the InstalledAdobeReaderPath property value will be used. /// </summary> public string AdobeReaderPath { set; get; } /// <summary> /// Returns the default printer name. /// </summary> public string DefaultPrinterName { get { var query = new ObjectQuery("SELECT * FROM Win32_Printer"); using (var searcher = new ManagementObjectSearcher(query)) { foreach (var mo in searcher.Get()) { if (((bool?)mo["Default"]) ?? false) return mo["Name"] as string; } } return string.Empty; } } /// <summary> /// The name and path of the PDF file to print. /// </summary> public string PdfFilePath { set; get; } /// <summary> /// Name of the printer such as '\\PrintServer\HP LaserJet'. /// If it's not specified, the DefaultPrinterName property value will be used. /// </summary> public string PrinterName { set; get; } /// <summary> /// Returns the HKEY_CLASSES_ROOT\Software\Adobe\Acrobat\Exe value. /// If AcroRd32.exe does not exist, returns string.Empty /// </summary> public string InstalledAdobeReaderPath { get { var acroRd32Exe = Registry.ClassesRoot.OpenSubKey(@"Software\Adobe\Acrobat\Exe", writable: false); if (acroRd32Exe == null) return string.Empty; var exePath = acroRd32Exe.GetValue(string.Empty) as string; if (string.IsNullOrEmpty(exePath)) return string.Empty; exePath = exePath.Trim(new[] { '"' }); return File.Exists(exePath) ? exePath : string.Empty; } } /// <summary> /// Executes the Adobe Reader and prints a file while suppressing the Acrobat print /// dialog box, then terminating the Reader. /// </summary> /// <param name="timeout">The amount of time, in milliseconds, to wait for the associated process to exit. The maximum is the largest possible value of a 32-bit integer, which represents infinity to the operating system.</param> public void PrintPdfFile(int timeout = Int32.MaxValue) { if (!File.Exists(PdfFilePath)) throw new ArgumentException(PdfFilePath + " does not exist."); var args = string.Format("/N /T \"{0}\" \"{1}\"", PdfFilePath, getPrinterName()); var process = startAdobeProcess(args); if (!process.WaitForExit(timeout)) process.Kill(); } private Process startAdobeProcess(string arguments = "") { var startInfo = new ProcessStartInfo { FileName = this.getExePath(), Arguments = arguments, CreateNoWindow = true, ErrorDialog = false, UseShellExecute = false, Verb = "print" }; return Process.Start(startInfo); } private string getPrinterName() { var printer = PrinterName; if (string.IsNullOrEmpty(printer)) printer = DefaultPrinterName; if (string.IsNullOrEmpty(printer)) throw new ArgumentException("Please set the PrinterName."); return printer; } private string getExePath() { var exePath = AdobeReaderPath; if (string.IsNullOrEmpty(exePath) || !File.Exists(exePath)) exePath = InstalledAdobeReaderPath; if (string.IsNullOrEmpty(exePath)) throw new ArgumentException("Please set the full path of the AcroRd32.exe or Acrobat.exe."); return exePath; } } }
توضیحات:
استفاده ابتدایی از کلاس فوق به نحو زیر است:
new AcroPrint { PdfFilePath = @"D:\path\test.pdf" }.PrintPdfFile();
ملاحظات:
- کدهای فوق نیاز به ارجاعی به اسمبلی استاندارد System.Management.dll نیز دارند.
- اگر علاقمند بودید که چاپگر خاصی را معرفی کنید (برای مثال یک چاپگر تعریف شده در شبکه)، میتوانید خاصیت PrinterName را مقدار دهی نمائید.
- محل نصب Adobe reader از رجیستری ویندوز استخراج میشود. اما اگر محل نصب برنامه استاندارد نبود، نیاز است خاصیت AdobeReaderPath مقدار دهی گردد.
- تحت هر شرایطی برنامه Adobe reader ظاهر خواهد شد؛ حتی اگر در حین آغاز پروسه سعی در مخفی کردن پنجره آن نمائید. اینکار به عمد جهت مسایل امنیتی در این برنامه درنظر گرفته شده است تا کاربر بداند که پروسه چاپ آغاز شده است.
روش پیاده سازی DDD
NET Core 2.2.4. منتشر شد
Etag چیست و چه کاربردی دارد
یافتن موقعیت جغرافیایی در HTML 5
کنفرانس مجازی blazor day
The blazor day is the online event around Blazor technologies. Originally, this event was organized by three MVP friends, Adrien, Christophe, and Denis. Their objective of this event is to share their passion for .NET and more particularly Blazor. Joined by the famous Charline to upgrade the event to the next level. Blazor is part of the ASP.NET Core product which is offered in Open Source by Microsoft....
Cloud Computing is currently the hot topic in the developer world these days, and it seems all anyone wants to talk about is the cloud. If you're like me you signed up for something like Windows Azure just to see what the hype was all about. There are a lot of good reasons to move an app to the cloud, but it's still not for everyone. There are some things you need to think about before taking this gamble with your app.