/// <summary> /// 读取INI文件中指定KEY的字符串型值 /// </summary> /// <param name="iniFile">Ini文件</param> /// <param name="section">节点名称</param> /// <param name="key">键名称</param> /// <param name="defaultValue">如果没此KEY所使用的默认值</param> /// <returns>读取到的值</returns> public static string INIGetStringValue(string iniFile, string section, string key, string defaultValue) { string value = defaultValue; const int SIZE = 1024 * 10; if (string.IsNullOrEmpty(section)) { throw new ArgumentException("必须指定节点名称", "section"); } if (string.IsNullOrEmpty(key)) { throw new ArgumentException("必须指定键名称(key)", "key"); } StringBuilder sb = new StringBuilder(SIZE); uint bytesReturned = INIHepler.GetPrivateProfileString(section, key, defaultValue, sb, SIZE, iniFile); if (bytesReturned != 0) { value = sb.ToString(); } sb = null; return(value); }
/// <summary> /// 在INI文件中,删除指定节点中的所有内容。 /// </summary> /// <param name="iniFile">INI文件</param> /// <param name="section">节点</param> /// <returns>操作是否成功</returns> public static bool INIEmptySection(string iniFile, string section) { if (string.IsNullOrEmpty(section)) { throw new ArgumentException("必须指定节点名称", "section"); } return(INIHepler.WritePrivateProfileSection(section, string.Empty, iniFile)); }
/// <summary> /// 在INI文件中,删除指定节点中的指定的键。 /// </summary> /// <param name="iniFile">INI文件</param> /// <param name="section">节点</param> /// <param name="key">键</param> /// <returns>操作是否成功</returns> public static bool INIDeleteKey(string iniFile, string section, string key) { if (string.IsNullOrEmpty(section)) { throw new ArgumentException("必须指定节点名称", "section"); } if (string.IsNullOrEmpty(key)) { throw new ArgumentException("必须指定键名称", "key"); } return(INIHepler.WritePrivateProfileString(section, key, null, iniFile)); }
/// <summary> /// 在INI文件中,将指定的键值对写到指定的节点,如果已经存在则替换 /// </summary> /// <param name="iniFile">INI文件</param> /// <param name="section">节点,如果不存在此节点,则创建此节点</param> /// <param name="items">键值对,多个用\0分隔,形如key1=value1\0key2=value2</param> /// <returns></returns> public static bool INIWriteItems(string iniFile, string section, string items) { if (string.IsNullOrEmpty(section)) { throw new ArgumentException("必须指定节点名称", "section"); } if (string.IsNullOrEmpty(items)) { throw new ArgumentException("必须指定键值对", "items"); } return(INIHepler.WritePrivateProfileSection(section, items, iniFile)); }
private void SetPrint(string sysiniPath, string reportTemplate) { //1.判断sysini配置文件是否存在 if (File.Exists(sysIniPath)) //存在 { //2.是否存在指定模板的打印机 var printName = INIHepler.INIGetStringValue(sysIniPath, "SysSetting", reportTemplate, null); if (string.IsNullOrEmpty(printName) || string.IsNullOrEmpty(LocalPrinter.GetPrinterByName(printName))) { PrintDocument printDocument = new PrintDocument(); //设置打印机,记录设置信息 PrintDialog printDialog = new PrintDialog(); printDialog.Document = printDocument; // printDialog.UseEXDialog = true; if (DialogResult.OK == printDialog.ShowDialog()) //如果确认,将会覆盖所有的打印参数设置 { //页面设置对话框(可以不使用,其实PrintDialog对话框已提供页面设置) PageSetupDialog psd = new PageSetupDialog(); psd.Document = printDocument; psd.ShowDialog(); } Externs.SetDefaultPrinter(printDocument.PrinterSettings.PrinterName); INIHepler.INIWriteValue(sysIniPath, "SysSetting", reportTemplate, printDocument.PrinterSettings.PrinterName); } } else { //3.不存在进行打印设置,并执行打印操作,记录设置信息 PrintDocument printDocument = new PrintDocument(); PrintDialog printDialog = new PrintDialog(); printDialog.Document = printDocument; // printDialog.UseEXDialog = true; if (DialogResult.OK == printDialog.ShowDialog()) //如果确认,将会覆盖所有的打印参数设置 { //页面设置对话框(可以不使用,其实PrintDialog对话框已提供页面设置) PageSetupDialog psd = new PageSetupDialog(); psd.Document = printDocument; psd.ShowDialog(); } Externs.SetDefaultPrinter(printDocument.PrinterSettings.PrinterName); INIHepler.INIWriteValue(sysIniPath, "SysSetting", reportTemplate, printDocument.PrinterSettings.PrinterName); } }
/// <summary> /// 在INI文件中,指定节点写入指定的键及值。如果已经存在,则替换。如果没有则创建。 /// </summary> /// <param name="iniFile">INI文件</param> /// <param name="section">节点</param> /// <param name="key">键</param> /// <param name="value">值</param> /// <returns>操作是否成功</returns> public static bool INIWriteValue(string iniFile, string section, string key, string value) { if (string.IsNullOrEmpty(section)) { throw new ArgumentException("必须指定节点名称", "section"); } if (string.IsNullOrEmpty(key)) { throw new ArgumentException("必须指定键名称", "key"); } if (value == null) { throw new ArgumentException("值不能为null", "value"); } return(INIHepler.WritePrivateProfileString(section, key, value, iniFile)); }
/// <summary> /// 获取INI文件中指定节点(Section)中的所有条目的Key列表 /// </summary> /// <param name="iniFile">Ini文件</param> /// <param name="section">节点名称</param> /// <returns>如果没有内容,反回string[0]</returns> public static string[] INIGetAllItemKeys(string iniFile, string section) { string[] value = new string[0]; const int SIZE = 1024 * 10; if (string.IsNullOrEmpty(section)) { throw new ArgumentException("必须指定节点名称", "section"); } char[] chars = new char[SIZE]; uint bytesReturned = INIHepler.GetPrivateProfileString(section, null, null, chars, SIZE, iniFile); if (bytesReturned != 0) { value = new string(chars).Split(new char[] { '\0' }, StringSplitOptions.RemoveEmptyEntries); } chars = null; return(value); }
/// <summary> /// 获取INI文件中指定节点(Section)中的所有条目(key=value形式) /// </summary> /// <param name="iniFile">Ini文件</param> /// <param name="section">节点名称</param> /// <returns>指定节点中的所有项目,没有内容返回string[0]</returns> public static string[] INIGetAllItems(string iniFile, string section) { //返回值形式为 key=value,例如 Color=Red uint MAX_BUFFER = 32767; //默认为32767 string[] items = new string[0]; //返回值 //分配内存 IntPtr pReturnedString = Marshal.AllocCoTaskMem((int)MAX_BUFFER * sizeof(char)); uint bytesReturned = INIHepler.GetPrivateProfileSection(section, pReturnedString, MAX_BUFFER, iniFile); if (!(bytesReturned == MAX_BUFFER - 2) || (bytesReturned == 0)) { string returnedString = Marshal.PtrToStringAuto(pReturnedString, (int)bytesReturned); items = returnedString.Split(new char[] { '\0' }, StringSplitOptions.RemoveEmptyEntries); } Marshal.FreeCoTaskMem(pReturnedString); //释放内存 return(items); }
/// <summary> /// 读取INI文件中指定INI文件中的所有节点名称(Section) /// </summary> /// <param name="iniFile">Ini文件</param> /// <returns>所有节点,没有内容返回string[0]</returns> public static string[] INIGetAllSectionNames(string iniFile) { uint MAX_BUFFER = 32767; //默认为32767 string[] sections = new string[0]; //返回值 //申请内存 IntPtr pReturnedString = Marshal.AllocCoTaskMem((int)MAX_BUFFER * sizeof(char)); uint bytesReturned = INIHepler.GetPrivateProfileSectionNames(pReturnedString, MAX_BUFFER, iniFile); if (bytesReturned != 0) { //读取指定内存的内容 string local = Marshal.PtrToStringAuto(pReturnedString, (int)bytesReturned).ToString(); //每个节点之间用\0分隔,末尾有一个\0 sections = local.Split(new char[] { '\0' }, StringSplitOptions.RemoveEmptyEntries); } //释放内存 Marshal.FreeCoTaskMem(pReturnedString); return(sections); }
private void startHttpThread() { using (HttpListener listerner = new HttpListener()) { listerner.AuthenticationSchemes = AuthenticationSchemes.Anonymous;//指定身份验证 Anonymous匿名访问 listerner.Prefixes.Add("http://127.0.0.1:" + port + "/report/"); listerner.Start(); log.Info("WebServer Start Successed......."); PrinterControl printerControl = new PrinterControl(); var reportTemplate = string.Empty; while (true) { //等待请求连接 //没有请求则GetContext处于阻塞状态 HttpListenerContext ctx = listerner.GetContext(); string callback = string.Empty; try { log.Info("开始处理请求…… "); Uri url = ctx.Request.Url; ctx.Response.StatusCode = 200;//设置返回给客服端http状态代码 ctx.Response.ContentType = "application/json"; Dictionary <string, string> parm = getData(ctx); if (parm.Count > 0) { var printUrl = parm["url"].ToString().Trim(); callback = parm["callback"].ToString().Trim(); log.Info(printUrl + ": " + printUrl); var postData = parm["postData"].ToString().Trim(); log.Info(postData + ": " + postData); var cookieStr = parm["cookieStr"].ToString().Trim(); log.Info(cookieStr + ": " + cookieStr); var printerParams = parm["printerParams"].ToString().Trim(); log.Info(printerParams + ": " + printerParams); var charset = parm["charset"].ToString().Trim(); log.Info(charset + ": " + charset); reportTemplate = parm["_report"].ToString().Trim(); log.Info(reportTemplate + ": " + reportTemplate); SetPrint(sysIniPath, reportTemplate); PrinterSettings settings = new PrinterSettings(); //settings.DefaultPageSettings.PaperSize.Kind= System.Drawing.Printing.PaperKind.Custom; //1.设置打印机名称 settings.PrinterName = INIHepler.INIGetStringValue(sysIniPath, reportTemplate, reportTemplate, null); //2.设置打印机打印方向 settings.DefaultPageSettings.Landscape = Convert.ToBoolean(INIHepler.INIGetStringValue(sysIniPath, reportTemplate, "SetLandscape", null)); //3.设置纸张 //settings.DefaultPageSettings.PaperSize.PaperName = INIHepler.INIGetStringValue(sysIniPath, reportTemplate, "SetPaperName", null); //纸张高度 var SetPaperHeight = INIHepler.INIGetStringValue(sysIniPath, reportTemplate, "SetPaperHeight", 0 + ""); //纸张宽度 var SetPaperWidth = INIHepler.INIGetStringValue(sysIniPath, reportTemplate, "SetPaperWidth", 0 + ""); PaperSize paperSize = new PaperSize(reportTemplate, Convert.ToInt32(SetPaperWidth), Convert.ToInt32(SetPaperHeight)); settings.DefaultPageSettings.PaperSize = paperSize; //4下边距 var SetMarginsBottom = INIHepler.INIGetStringValue(sysIniPath, reportTemplate, "SetMarginsBottom", 0 + ""); //5上边距 var SetMarginsTop = INIHepler.INIGetStringValue(sysIniPath, reportTemplate, "SetMarginsTop", 0 + ""); //6左边距 var SetMarginsLeft = INIHepler.INIGetStringValue(sysIniPath, reportTemplate, "SetMarginsLeft", 0 + ""); //7右边距 var SetMarginsRight = INIHepler.INIGetStringValue(sysIniPath, reportTemplate, "SetMarginsRight", 0 + ""); //8------设置边距 Margins margins = new Margins(); margins.Bottom = Convert.ToInt32(SetMarginsBottom); margins.Top = Convert.ToInt32(SetMarginsTop); margins.Left = Convert.ToInt32(SetMarginsLeft); margins.Right = Convert.ToInt32(SetMarginsRight); settings.DefaultPageSettings.Margins = margins; var result = printerControl.SilentPrint(printUrl, postData, cookieStr, printerParams, charset, settings); log.Info("本地打印服务调用打印操作:" + result); using (StreamWriter writer = new StreamWriter(ctx.Response.OutputStream)) { // writer.WriteLine(result); writeJS(writer, result.ToString(), callback); } } } catch (Exception e) { if (ctx != null) { ctx.Response.StatusCode = 500;//设置返回给客服端http状态代码 using (StreamWriter writer = new StreamWriter(ctx.Response.OutputStream)) { writeJS(writer, "{status:'error',msg:'" + e.StackTrace + "'}", callback); log.Info("调用本地打印服务出现异常" + e.StackTrace + "*******" + e.Message + "*******" + e.InnerException.StackTrace); } } } } } }
private void SetPrint(string sysiniPath, string reportTemplate) { //1.判断sysini配置文件是否存在 if (File.Exists(sysIniPath)) //存在 { //2.是否存在指定模板的打印机 var printName = INIHepler.INIGetStringValue(sysIniPath, reportTemplate, reportTemplate, null); if (string.IsNullOrEmpty(printName) || string.IsNullOrEmpty(LocalPrinter.GetPrinterByName(printName))) { PrintDocument printDocument = new PrintDocument(); //设置打印机,记录设置信息 PrintDialog printDialog = new PrintDialog(); printDialog.Document = printDocument; printDialog.UseEXDialog = true; printDocument.OriginAtMargins = true; //printDocument.DefaultPageSettings.PaperSize.Kind = System.Drawing.Printing.PaperKind.Custom; this.Invoke(new Action(() => { var result = printDialog.ShowDialog(); if (DialogResult.OK == result) //如果确认,将会覆盖所有的打印参数设置 { //页面设置对话框(可以不使用,其实PrintDialog对话框已提供页面设置) PageSetupDialog psd = new PageSetupDialog(); psd.Document = printDocument; psd.ShowDialog(); } //1设置默认打印机 Externs.SetDefaultPrinter(printDocument.PrinterSettings.PrinterName); //2设置打印机名称 INIHepler.INIWriteValue(sysIniPath, reportTemplate, reportTemplate, printDocument.PrinterSettings.PrinterName); //3设置打印机打印方向 INIHepler.INIWriteValue(sysIniPath, reportTemplate, "SetLandscape", printDocument.DefaultPageSettings.Landscape.ToString()); //4设置纸张名称 A3等 INIHepler.INIWriteValue(sysIniPath, reportTemplate, "SetPaperName", printDocument.DefaultPageSettings.PaperSize.PaperName); //5设置下边距 INIHepler.INIWriteValue(sysIniPath, reportTemplate, "SetMarginsBottom", printDocument.DefaultPageSettings.Margins.Bottom.ToString()); //6设置上边距 INIHepler.INIWriteValue(sysIniPath, reportTemplate, "SetMarginsTop", printDocument.DefaultPageSettings.Margins.Top.ToString()); //7设置左边距 INIHepler.INIWriteValue(sysIniPath, reportTemplate, "SetMarginsLeft", printDocument.DefaultPageSettings.Margins.Left.ToString()); //8设置右边距 INIHepler.INIWriteValue(sysIniPath, reportTemplate, "SetMarginsRight", printDocument.DefaultPageSettings.Margins.Right.ToString()); //9设置纸张高度 INIHepler.INIWriteValue(sysIniPath, reportTemplate, "SetPaperHeight", printDocument.DefaultPageSettings.PaperSize.Height.ToString()); //10设置纸张宽度 INIHepler.INIWriteValue(sysIniPath, reportTemplate, "SetPaperWidth", printDocument.DefaultPageSettings.PaperSize.Width.ToString()); })); } } else //不存在打印设置 { //3.不存在进行打印设置,并执行打印操作,记录设置信息 PrintDocument printDocument = new PrintDocument(); PrintDialog printDialog = new PrintDialog(); printDialog.Document = printDocument; printDialog.UseEXDialog = true; printDocument.OriginAtMargins = true; // printDocument.DefaultPageSettings.PaperSize.Kind = System.Drawing.Printing.PaperKind.Custom; this.Invoke(new Action(() => { var result = printDialog.ShowDialog(); if (DialogResult.OK == result) //如果确认,将会覆盖所有的打印参数设置 { //页面设置对话框(可以不使用,其实PrintDialog对话框已提供页面设置) PageSetupDialog psd = new PageSetupDialog(); psd.Document = printDocument; psd.ShowDialog(); } })); //1设置默认打印机 Externs.SetDefaultPrinter(printDocument.PrinterSettings.PrinterName); //2设置打印机名称 INIHepler.INIWriteValue(sysIniPath, reportTemplate, reportTemplate, printDocument.PrinterSettings.PrinterName); //3设置打印机打印方向 INIHepler.INIWriteValue(sysIniPath, reportTemplate, "SetLandscape", printDocument.DefaultPageSettings.Landscape.ToString()); //4设置纸张名称 A3等 INIHepler.INIWriteValue(sysIniPath, reportTemplate, "SetPaperName", reportTemplate); //5设置下边距 INIHepler.INIWriteValue(sysIniPath, reportTemplate, "SetMarginsBottom", printDocument.DefaultPageSettings.Margins.Bottom.ToString()); //6设置上边距 INIHepler.INIWriteValue(sysIniPath, reportTemplate, "SetMarginsTop", printDocument.DefaultPageSettings.Margins.Top.ToString()); //7设置左边距 INIHepler.INIWriteValue(sysIniPath, reportTemplate, "SetMarginsLeft", printDocument.DefaultPageSettings.Margins.Left.ToString()); //8设置右边距 INIHepler.INIWriteValue(sysIniPath, reportTemplate, "SetMarginsRight", printDocument.DefaultPageSettings.Margins.Right.ToString()); //9设置纸张高度 INIHepler.INIWriteValue(sysIniPath, reportTemplate, "SetPaperHeight", printDocument.DefaultPageSettings.PaperSize.Height.ToString()); //10设置纸张宽度 INIHepler.INIWriteValue(sysIniPath, reportTemplate, "SetPaperWidth", printDocument.DefaultPageSettings.PaperSize.Width.ToString()); } }