你可以做这样的事情......
注:这是示例代码如何使用C#窗口服务打印PDF文件,如果你要打印的文本文件,你可以更改此代码
带有按钮(cmdGetPrinters)和ListView的Windows窗体 (lstPrinters)。列表视图有两列定义 - “属性”和 “值”,它将描述安装在本地 机器上的打印机。下面的代码实现了魔术。
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()
)
})
);
}
}
}
}
此图显示了这个小窗口应用程序的结果。请注意“名称”属性,这将用于将文本文件发送到打印机。这显示在打印机的“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){ }
}
我希望这将有助于你