C# 如何自动打印
C# 如何自动打印 [.NET技术 C#]
------解决方案--------------------你是指用c#编程?还是问的普通的办公问题?普通办公问题:麻烦你打开“苏州001.doc”,文件->打印->选择好相应城市的打印机->打印编程问题:获取文件名中的城市名你会吧,然后根据城市名得到打印机名(这一步只能你手动设定,比如苏州(城市名)对应打印机名是001,你也可以把对应的打印机名设为城市名 这样就ok了)然后运用打印类: public class RawPrinterHelper { // [Methods] [DllImport("winspool.Drv", CallingConvention = CallingConvention.StdCall, SetLastError = true, ExactSpelling = true)] public static extern bool ClosePrinter(IntPtr hPrinter); [DllImport("winspool.Drv", CallingConvention = CallingConvention.StdCall, SetLastError = true, ExactSpelling = true)] public static extern bool EndDocPrinter(IntPtr hPrinter); [DllImport("winspool.Drv", CallingConvention = CallingConvention.StdCall, SetLastError = true, ExactSpelling = true)] public static extern bool EndPagePrinter(IntPtr hPrinter); [DllImport("winspool.Drv", EntryPoint = "OpenPrinterA", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)] public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out IntPtr hPrinter, IntPtr pd); public static bool SendBytesToPrinter(string szPrinterName, IntPtr pBytes, int dwCount) { int dwError = 0; int dwWritten = 0; IntPtr hPrinter = new IntPtr(0); DOCINFOA di = new DOCINFOA(); bool bSuccess = false; di.pDocName = "My C#.NET RAW Document"; di.pDataType = "RAW"; if (OpenPrinter(szPrinterName.Normalize(), out hPrinter, IntPtr.Zero)) { if (StartDocPrinter(hPrinter, 1, di)) { if (StartPagePrinter(hPrinter)) { bSuccess = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten); EndPagePrinter(hPrinter); } EndDocPrinter(hPrinter); } ClosePrinter(hPrinter); } if (!bSuccess) { dwError = Marshal.GetLastWin32Error(); } return bSuccess; } public static bool SendFileToPrinter(string szPrinterName, string szFileName) { FileStream fs = new FileStream(szFileName, FileMode.Open); BinaryReader br = new BinaryReader(fs); byte[] bytes = new byte[fs.Length]; bool bSuccess = false; IntPtr pUnmanagedBytes = new IntPtr(0); int nLength = Convert.ToInt32(fs.Length); bytes = br.ReadBytes(nLength); pUnmanagedBytes = Marshal.AllocCoTaskMem(nLength); Marshal.Copy(bytes, 0, pUnmanagedBytes, nLength); bSuccess = SendBytesToPrinter(szPrinterName, pUnmanagedBytes, nLength); Marshal.FreeCoTaskMem(pUnmanagedBytes); return bSuccess; } public static bool SendStringToPrinter(string szPrinterName, string szString) { int dwCount = szString.Length; IntPtr pBytes = Marshal.StringToCoTaskMemAnsi(szString); SendBytesToPrinter(szPrinterName, pBytes, dwCount); Marshal.FreeCoTaskMem(pBytes); return true; } [DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)] public static extern bool StartDocPrinter(IntPtr hPrinter, int level, [In, MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di); [DllImport("winspool.Drv", CallingConvention = CallingConvention.StdCall, SetLastError = true, ExactSpelling = true)] public static extern bool StartPagePrinter(IntPtr hPrinter); [DllImport("winspool.Drv", CallingConvention = CallingConvention.StdCall, SetLastError = true, ExactSpelling = true)] public static extern bool WritePrinter(IntPtr hPrinter, IntPtr pBytes, int dwCount, out int dwWritten); //Nested Types [StructLayout(LayoutKind.Sequential)] public class DOCINFOA { [MarshalAs(UnmanagedType.LPStr)] public string pDocName; [MarshalAs(UnmanagedType.LPStr)] public string pOutputFile; [MarshalAs(UnmanagedType.LPStr)] public string pDataType; } }这是网上的一个比较标准的打印类,比较好用,直接copy然后:SendFileToPrinter(打印机名,文件名).