private void btnPrint_Click(object sender, EventArgs e)
{
try
{
//以tbJB这个文本框的值来查找脚本文件
string strline;
FileStream afile = new FileStream("" + tbJB.Text + "", FileMode.Open);
//MessageBox.Show("当前打开的文件"+tbJB.Text);
StreamWriter sw = new StreamWriter(afile); //读取脚本文件并用界面的值替换脚本中对应的变量
StreamReader sr = new StreamReader(afile);
strline = sr.ReadToEnd();
sw.Close();
string a = strline; a = a.Replace("$QSSN$", this.tbSN.Text);
a = a.Replace("$FLASH$", this.tbFlash.Text);
a = a.Replace("$PD$", this.cbPD.Text);
a = a.Replace("$JZ$", this.tbJZ.Text);
a = a.Replace("$SL$", this.tbSL.Text);
//a = a.Replace("$HW$", this.textBox2.Text);
//a = a.Replace("$SW$", this.textBox4.Text);
//a = a.Replace("$WEEK$", this.label10.Text);
//a = a.Replace("$YEAR$", DateTime.Now.Year.ToString().Remove(0, 2)); FileStream bfile = new FileStream("label17.txt", FileMode.Create);
StreamWriter ssw = new StreamWriter(bfile);
ssw.WriteLine(a); ssw.Close(); //把脚本推送到打印机
Process p = new Process(); p.StartInfo.FileName = "cmd.exe"; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; p.StartInfo.CreateNoWindow = true; string pingrst;
p.Start(); p.StandardInput.WriteLine("copy label.txt lpt1"); p.StandardInput.WriteLine("exit"); string strRst = p.StandardOutput.ReadToEnd(); if (strRst.IndexOf("(0% loss)") != -1)
{
pingrst = "连接";
} else if (strRst.IndexOf("Destination host unreachable.") != -1)
{
pingrst = "无法到达目的主机";
} else if (strRst.IndexOf("Request timed out.") != -1)
{
pingrst = "超时";
}
else if (strRst.IndexOf("Unknown host") != -1)
{
pingrst = "无法解析主机";
}
else
{
pingrst = strRst;
}
p.Close();
MessageBox.Show("打印完成!");
}
catch (IOException ex)
{
MessageBox.Show(ex.ToString().Trim());
}
}
这是我写的打印,但是现在有点问题请教各位 因为想在有两台打印我都要使用 但是 我写的 只能通过其中一台打印机打印,我请各位大神帮我改造下代码 让这段代码可以通过不同的打印端口打印 两台打印机的端口 分别为LPT1 LPT3
{
try
{
//以tbJB这个文本框的值来查找脚本文件
string strline;
FileStream afile = new FileStream("" + tbJB.Text + "", FileMode.Open);
//MessageBox.Show("当前打开的文件"+tbJB.Text);
StreamWriter sw = new StreamWriter(afile); //读取脚本文件并用界面的值替换脚本中对应的变量
StreamReader sr = new StreamReader(afile);
strline = sr.ReadToEnd();
sw.Close();
string a = strline; a = a.Replace("$QSSN$", this.tbSN.Text);
a = a.Replace("$FLASH$", this.tbFlash.Text);
a = a.Replace("$PD$", this.cbPD.Text);
a = a.Replace("$JZ$", this.tbJZ.Text);
a = a.Replace("$SL$", this.tbSL.Text);
//a = a.Replace("$HW$", this.textBox2.Text);
//a = a.Replace("$SW$", this.textBox4.Text);
//a = a.Replace("$WEEK$", this.label10.Text);
//a = a.Replace("$YEAR$", DateTime.Now.Year.ToString().Remove(0, 2)); FileStream bfile = new FileStream("label17.txt", FileMode.Create);
StreamWriter ssw = new StreamWriter(bfile);
ssw.WriteLine(a); ssw.Close(); //把脚本推送到打印机
Process p = new Process(); p.StartInfo.FileName = "cmd.exe"; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; p.StartInfo.CreateNoWindow = true; string pingrst;
p.Start(); p.StandardInput.WriteLine("copy label.txt lpt1"); p.StandardInput.WriteLine("exit"); string strRst = p.StandardOutput.ReadToEnd(); if (strRst.IndexOf("(0% loss)") != -1)
{
pingrst = "连接";
} else if (strRst.IndexOf("Destination host unreachable.") != -1)
{
pingrst = "无法到达目的主机";
} else if (strRst.IndexOf("Request timed out.") != -1)
{
pingrst = "超时";
}
else if (strRst.IndexOf("Unknown host") != -1)
{
pingrst = "无法解析主机";
}
else
{
pingrst = strRst;
}
p.Close();
MessageBox.Show("打印完成!");
}
catch (IOException ex)
{
MessageBox.Show(ex.ToString().Trim());
}
}
这是我写的打印,但是现在有点问题请教各位 因为想在有两台打印我都要使用 但是 我写的 只能通过其中一台打印机打印,我请各位大神帮我改造下代码 让这段代码可以通过不同的打印端口打印 两台打印机的端口 分别为LPT1 LPT3
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货