
新接到需求,wcf客户端程序运行时,能实时修改程序的打印机名称;
使用XmlHelper读写 winform.exe.config文件修改后始终,不能实时读取出来,查询博客园,原来已有大神解释了;
获取电脑连接的打印机列表需要引入using System.Drawing.Printing;
#region GetPrinters
private List<string> GetPrinters()
{
var printerNames = new List<string>();
foreach (var printer in PrinterSettings.InstalledPrinters)
{
printerNames.Add(printer.ToString());
}
return printerNames;
}
#endregion
指定打印机列表默认显示打印机为配置文件中的打印机:
printerNames.ForEach(o =>
{
if (defaultPrinter == o)
cbxPrinterList.SelectedIndex = printerNames.IndexOf(o);
});
确定按钮修改默认打印机名称
#region 判断是否有key
if (ConfigurationManager.AppSettings["DefaultPrinterName"] == null)
{
XmlHelper.UpdateKey("DefaultPrinterName", "ZDesigner GT800-300dpi EPL", WMSWinConstant.ApplicatonName + ".exe");
}
else
{
var printerName = cbxPrinterList.Text; //string appDomainConfigFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("appSettings");
appSettings.Settings["DefaultPrinterName"].Value = printerName;
config.Save();
//重点这句
ConfigurationManager.RefreshSection("appSettings");
}
#endregion
获取默认打印机 public static PrintDocument fPrintDocument = new PrintDocument(); //获取本机默认打印机名称
public static String DefaultPrinter()
{
return fPrintDocument.PrinterSettings.PrinterName;
}