如何使用c#窗口服务打印文本文件
本文关键字:打印 文本 文件 服务 窗口 何使用 | 更新日期: 2023-09-27 18:11:57
如何通过打印机自动打印基于给定文件名的文本文件,而不需要在C sharp窗口服务中进行任何手工操作,它不适合我,有人给我建议吗?
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++;
}
你可以这样做…
注意:这是如何使用c#窗口服务打印pdf文件的示例代码,如果你想打印文本文件,你可以修改这段代码
Windows窗体带有一个按钮(cmdGetPrinters)和一个列表视图(lstPrinters)。列表视图定义了两列——"Property"answers"值",用来描述安装在本地的打印机机器。下面的代码实现了这个神奇的功能。using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data; System.Drawing;
using System.Text; System.Windows.Forms;
using System.Management;
using System.Management.Instrumentation;
public partial class frmPrintDisplay : Form
{
public frmPrintDisplay()
{
InitializeComponent();
}
private void cmdGetPrinters_Click(object sender, EventArgs e)
{
ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_Printer");
ManagementObjectSearcher mo = new ManagementObjectSearcher(query);
ManagementObjectCollection printers = mo.Get();
foreach (ManagementObject printer in printers)
{
PropertyDataCollection printerProperties = printer.Properties;
string printerPath = printer.Path.ToString() ;
PropertyDataCollection.PropertyDataEnumerator test =
printer.Properties.GetEnumerator();
while(! (test.MoveNext()== false ))
{
lstPrinters.Items.Add(
new ListViewItem( new string[]
{
test.Current.Name,
(
(test.Current.Value == null) ?
"n/a" : test.Current.Value.ToString()
)
})
);
}
}
}
}
这个图像显示了这个小的windows应用程序的结果。注意"Name"属性,这将用于将文本文件发送到打印机。这显示在打印机的"ShareName"属性下。在同样位于同一域/工作组的测试机器上,您必须安装一台新的网络打印机,并将安装指向第一台计算机上的共享打印机。这实际上使第一台计算机成为打印机服务器,并且您能够为客户机密切复制设置。
现在到测试机器上…上面提到的应用程序code building
将文本文件保存到一个临时目录C:'Program files[应用程序名称]'Temp'
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data; System.Drawing;
using System.IO;
using System.Text; System.Windows.Forms;
using System.Management;
using System.Management.Instrumentation;
private void print(ref DirectoryInfo tempDir)
{
try
{
foreach(FileInfo file in tempDir.GetFiles("*.pdf"))
{
file.CopyTo("''''XYZ''Phaser77''" + file.Name);
}
}
catch(Exception ee){ }
}
希望对你有帮助