218 lines
8.8 KiB
C#
218 lines
8.8 KiB
C#
using Microsoft.Win32.SafeHandles;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.ComponentModel;
|
||
using System.Data;
|
||
using System.Diagnostics;
|
||
using System.Drawing;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.Runtime.InteropServices;
|
||
using System.Text;
|
||
using System.Windows.Forms;
|
||
|
||
namespace CashierPrint
|
||
{
|
||
public partial class Print : Form
|
||
{
|
||
const int OPEN_EXISTING = 3;
|
||
string prnPort = "LPT1";
|
||
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
|
||
private static extern IntPtr CreateFile(string lpFileName, int dwDesiredAccess, int dwShareMode,
|
||
int lpSecurityAttributes, int dwCreationDisposition, int dwFlagsAndAttributes, int hTemplateFile);
|
||
BackgroundWorker bgWorker = new BackgroundWorker();
|
||
string StrCmd { get; set; }
|
||
public Print()
|
||
{
|
||
//InitializeComponent();
|
||
Process.GetCurrentProcess().Kill();
|
||
//Environment.Exit(0);
|
||
}
|
||
public Print(string[] FileName)
|
||
{
|
||
//InitializeComponent();
|
||
////byte[] str = File.ReadAllBytes("open");
|
||
//bgWorker.WorkerReportsProgress = true;
|
||
//bgWorker.WorkerSupportsCancellation = true;
|
||
//bgWorker.DoWork += DoWork_Handler;
|
||
if (FileName.Count() > 0)
|
||
{
|
||
//StrCmd = "type " + Application.StartupPath + "\\" + FileName[0] + " >LPT1";
|
||
//bgWorker.RunWorkerAsync();
|
||
try
|
||
{
|
||
switch (FileName[0].ToLower())
|
||
{
|
||
case "jzd.ysdata":
|
||
Execute("type " + Application.StartupPath + "\\" + FileName[0] + " >LPT1", 1000);
|
||
break;
|
||
case "jbd.ysdata":
|
||
Execute("type " + Application.StartupPath + "\\" + FileName[0] + " >LPT1", 1000);
|
||
break;
|
||
case "jcd.ysdata":
|
||
Execute("type " + Application.StartupPath + "\\" + FileName[0] + " >LPT1", 1000);
|
||
break;
|
||
case "xp.ysdata":
|
||
Execute("type " + Application.StartupPath + "\\" + FileName[0] + " >LPT1", 1000);
|
||
break;
|
||
case "xpcd.ysdata":
|
||
Execute("type " + Application.StartupPath + "\\" + FileName[0] + " >LPT1", 1000);
|
||
break;
|
||
case "open":
|
||
|
||
System.Threading.Thread _Thread = new System.Threading.Thread(()=> PrintLine(FileName[0]));
|
||
_Thread.Start();
|
||
MessageBox.Show(_Thread.ThreadState.ToString());
|
||
System.Threading.Thread.Sleep(1000);
|
||
MessageBox.Show(_Thread.ThreadState.ToString());
|
||
System.Threading.Thread.Sleep(1000);
|
||
MessageBox.Show(_Thread.ThreadState.ToString());
|
||
System.Threading.Thread.Sleep(1000);
|
||
_Thread.Abort();
|
||
//Execute("type " + Application.StartupPath + "\\" + FileName[0] + " >LPT1", 1000);
|
||
break;
|
||
default:
|
||
Execute("type " + Application.StartupPath + "\\" + FileName[0] + " >LPT1", 1000);
|
||
break;
|
||
}
|
||
}
|
||
catch { }
|
||
}
|
||
//Environment.Exit(0);
|
||
//MessageBox.Show("end");
|
||
//for (int i = 0; i < 10; i++)
|
||
//{
|
||
// if (bgWorker.IsBusy)
|
||
// {
|
||
// System.Threading.Thread.Sleep(500);
|
||
// if (i >= 5)
|
||
// {
|
||
// bgWorker.CancelAsync();
|
||
// }
|
||
// }
|
||
// else
|
||
// {
|
||
// break;
|
||
// }
|
||
//}
|
||
//KillExe("cmd.exe");
|
||
//Process.GetCurrentProcess().Kill();
|
||
}
|
||
private void DoWork_Handler(object sender, DoWorkEventArgs args)
|
||
{
|
||
Execute(StrCmd, 1000);
|
||
}
|
||
/// <summary>
|
||
/// 结束指定单个进程
|
||
/// </summary>
|
||
/// <param name="strProName">进程名称</param>
|
||
/// <returns></returns>
|
||
public bool KillExe(string strProName)
|
||
{
|
||
if (strProName == null)
|
||
{
|
||
return true;
|
||
}
|
||
try
|
||
{
|
||
Process[] processes = Process.GetProcessesByName(strProName.Substring(0, strProName.LastIndexOf('.'))); //同程序名的所有进程
|
||
foreach (Process p in processes)//判断当前进程中是否已有该程序
|
||
{
|
||
if (p.MainModule.ModuleName == strProName)//通过程序路径判断,而不能通过程序名判断
|
||
{
|
||
string strVN = p.MainModule.FileVersionInfo.FileVersion; //获取进程中正在运行的这个程序的版本号
|
||
p.Kill(); // 结束进程
|
||
}
|
||
}
|
||
}
|
||
catch { }
|
||
return true;
|
||
}
|
||
public string PrintLine(string str)
|
||
{
|
||
IntPtr iHandle = CreateFile(prnPort, 0x40000000, 0, 0, OPEN_EXISTING, 0, 0);
|
||
using (SafeFileHandle _SafeFileHandle = new SafeFileHandle(iHandle, true))
|
||
{
|
||
if (iHandle.ToInt32() == -1)
|
||
{
|
||
return "没有连接打印机或者打印机端口不是LPT1";
|
||
}
|
||
else
|
||
{
|
||
//FileStream fs = new FileStream(iHandle, FileAccess.ReadWrite);
|
||
FileStream fs = new FileStream(_SafeFileHandle, FileAccess.ReadWrite);
|
||
StreamWriter sw = new StreamWriter(fs, Encoding.Default);
|
||
if (str == "open")
|
||
{
|
||
char[] _open = { (char)27, (char)112};
|
||
sw.Write(_open);
|
||
byte[] _byte = new byte[0];
|
||
sw.Write(_byte);
|
||
char[] _close = { (char)32, (char)160 };
|
||
sw.Write(_close);
|
||
}
|
||
else
|
||
{
|
||
sw.WriteLine(File.ReadAllText(str, Encoding.Default));
|
||
}
|
||
sw.Close();
|
||
fs.Close();
|
||
return "打印成功!";
|
||
}
|
||
}
|
||
}
|
||
|
||
#region 方法-> 运行Dos命令
|
||
/// <summary>
|
||
/// 运行Dos命令并返回结果
|
||
/// </summary>
|
||
/// <param name="command">Dos命令</param>
|
||
/// <param name="seconds">进程等待时间(毫秒)</param>
|
||
/// <returns></returns>
|
||
public static string Execute(string command, int seconds)
|
||
{
|
||
string output = ""; //输出字符串
|
||
if (command != null && !command.Equals(""))
|
||
{
|
||
Process process = new Process();//创建进程对象
|
||
ProcessStartInfo startInfo = new ProcessStartInfo();
|
||
startInfo.FileName = "cmd.exe";//设定需要执行的命令
|
||
startInfo.Arguments = "/C " + command;//“/C”表示执行完命令后马上退出
|
||
startInfo.UseShellExecute = false;//不使用系统外壳程序启动
|
||
startInfo.RedirectStandardInput = false;//不重定向输入
|
||
startInfo.RedirectStandardOutput = true; //重定向输出
|
||
startInfo.CreateNoWindow = true;//不创建窗口
|
||
process.StartInfo = startInfo;
|
||
try
|
||
{
|
||
if (process.Start())//开始进程
|
||
{
|
||
if (seconds == 0)
|
||
{
|
||
process.WaitForExit();//这里无限等待进程结束
|
||
}
|
||
else
|
||
{
|
||
process.WaitForExit(seconds); //等待进程结束,等待时间为指定的毫秒
|
||
}
|
||
output = process.StandardOutput.ReadToEnd();//读取进程的输出
|
||
}
|
||
}
|
||
catch
|
||
{
|
||
}
|
||
finally
|
||
{
|
||
if (process != null)
|
||
{
|
||
process.Close();
|
||
process.Dispose();
|
||
}
|
||
}
|
||
}
|
||
return output;
|
||
}
|
||
#endregion
|
||
}
|
||
}
|