如何通過打印機自動打印基於給定文件名的文本文件,而無需在C sharp窗口服務中執行任何手動手動工作,它不是爲我工作,任何人給我建議。如何通過使用C#窗口服務通過打印機打印數據打印文本文件
using System.Management;
private Font printFont;
private StreamReader streamToPrint;
private void GetPrinters(string fileName)
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Printer");
string printerName = "";
foreach (ManagementObject printer in searcher.Get())
{
printerName = printer["Name"].ToString().ToLower();
if (printerName.Equals(@"\\chenraqdc1.raqmiyat.local\hp laserjet black chennai"))
{
Console.WriteLine("Printer = " + printer["Name"]);
if (printer["WorkOffline"].ToString().ToLower().Equals("true"))
{
// printer is offline by user
label1.Text = "Your Plug-N-Play printer is not connected.";
}
else
{streamToPrint = new StreamReader(fileName);
printFont = new Font("Arial", 10);
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler
(this.pd_PrintPage);
pd.Print();
streamToPrint.Close();
}
}
}
}
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
float linesPerPage = 0;
float yPos = 0;
int count = 0;
float leftMargin = ev.MarginBounds.Left;
float topMargin = ev.MarginBounds.Top;
string line = null;
// Calculate the number of lines per page.
linesPerPage = ev.MarginBounds.Height/
printFont.GetHeight(ev.Graphics);
// Print each line of the file.
while (count < linesPerPage &&
((line = streamToPrint.ReadLine()) != null))
{
yPos = topMargin + (count *
printFont.GetHeight(ev.Graphics));
ev.Graphics.DrawString(line, printFont, Brushes.Black,
leftMargin, yPos, new StringFormat());
count++;
}
我是否需要設置任何打印機設置,y它沒有得到打印 – pravz
代碼是不會拋出任何錯誤,pd.Print();系統來到這條線它顯示一個小的對話框打印文件,但文件沒有得到打印,任何方式善意地幫助如何禁用該打印對話框 – pravz