你可以使用printui来实现此功能,将打印机设置为双面打印,然后使用printui /Ss将设置保存到文件中。浏览各种选项,例如2、7、d等,看哪个文件保存了双面设置。
另一种方法是使用打印机API,并使用PRINTER_INFO_8(全局)或PRINTER_INFO_9(用户)结构体 - {{link1:MSDN链接}}。然后使用DEVMODE结构设置dmDuplex为双面。
首先打开打印机,完成后关闭它。
[DllImport("winspool.drv", EntryPoint = "OpenPrinter", SetLastError = true)]
internal static extern bool OpenPrinter(string pPrinterName, ref IntPtr phPrinter, PRINTER_DEFAULTS pDefault)
[DllImport("winspool.drv", EntryPoint = "ClosePrinter", SetLastError = true)]
internal static extern int ClosePrinter(IntPtr hPrinter)
使用 GetPrinter 获取当前打印机设置,使用 SetPrinter 将修改后的设置放回去:
[DllImport("winspool.drv", CharSet = CharSet.Auto, EntryPoint = "GetPrinter", SetLastError = true)]
internal static extern bool GetPrinter(IntPtr hPrinter, uint Level, IntPtr pPrinter, uint cbBuf, out uint pcbNeeded);
[DllImport("winspool.drv", CharSet = CharSet.Auto, EntryPoint = "SetPrinter", SetLastError = true)]
internal static extern bool SetPrinter(IntPtr hPrinter, uint Level, IntPtr pPrinter, uint Command);
实际执行此操作的代码应该如下所示:
var oldPrinter = printerFunctions.OpenPrinterHandle(String.Format(@"\", oldServerName, oldPrinterName));
var printerInfo8 = printerFunctions.GetPrinterInfo<PRINTER_INFO_8>(oldPrinter, 8);
printerFunctions.SetPrinter(oldPrinter, printerInfo8, 8);
printerFunctions.ClosePrinterHandle(oldPrinter);
public IntPtr OpenPrinterHandle(string printerName)
;
var hPrinter = IntPtr.Zero;
if (!PrinterNativeMethods.OpenPrinter(printerName, ref hPrinter, def))
return hPrinter;
}
public void ClosePrinterHandle(IntPtr hPrinter)
public void SetPrinter<T>(IntPtr hPrinter, T printerInfo, uint level)
Marshal.FreeHGlobal(printerInfoPtr);
}
public T GetPrinterInfo<T>(IntPtr hPrinter, uint level)
var currentPrinterInfoPtr = Marshal.AllocHGlobal((int)pcbNeeded);
bFlag = PrinterNativeMethods.GetPrinter(hPrinter, level, currentPrinterInfoPtr, pcbNeeded, out pcbNeeded);
if (!bFlag)
return (T)Marshal.PtrToStructure(currentPrinterInfoPtr, typeof(T));
}