using System; using System.IO; using System.Windows.Forms; using System.Xml; namespace OS.Spin.Common.Files { public class ConfigHelper { private static XmlDocument doc = null; private static ConfigHelper _helper = null; public static ConfigHelper GetInstance() { return _helper ?? (_helper = new ConfigHelper()); } /// /// 修改配置文件信息 /// public bool SetConfig(ParamEntity entity) { try { doc = new XmlDocument(); doc.Load(Path.Combine(Application.StartupPath, @"config\ParaSettings.xml")); doc.SelectSingleNode("root/prob_cls").InnerText = entity.Prob_cls.ToString(); doc.SelectSingleNode("root/prob_sma").InnerText = entity.Prob_sma.ToString(); doc.SelectSingleNode("root/prob_color").InnerText = entity.Prob_color.ToString(); doc.SelectSingleNode("root/prob_thin").InnerText = entity.Prob_thin.ToString(); doc.SelectSingleNode("root/len_sma").InnerText = entity.Len_sma.ToString(); doc.SelectSingleNode("root/m_base").InnerText = entity.M_base.ToString(); doc.SelectSingleNode("root/thre_area").InnerText = entity.Thre_Area.ToString(); doc.SelectSingleNode("root/thre_location").InnerText = entity.Thre_Location.ToString(); doc.SelectSingleNode("root/index_shift").InnerText = entity.Index_shift.ToString(); doc.SelectSingleNode("root/thresh_ratio").InnerText = entity.Thresh_ratio.ToString(); doc.SelectSingleNode("root/num_ratio").InnerText = entity.Num_ratio.ToString(); doc.Save(Path.Combine(Application.StartupPath, @"config\ParaSettings.xml")); } catch (Exception ex) { LogisTrac.WriteLog(string.Format("SetConfig:{0}", ex.Message)); return false; } return true; } /// /// 获取配置文件信息 /// /// /// public ParamEntity GetConfig() { try { doc = new XmlDocument(); doc.Load(Path.Combine(Application.StartupPath, @"config\ParaSettings.xml")); ParamEntity entity = new ParamEntity(); entity.Prob_cls = Convert.ToSingle(doc.SelectSingleNode("root/prob_cls").InnerText); entity.Prob_sma = Convert.ToSingle(doc.SelectSingleNode("root/prob_sma").InnerText); entity.Prob_color = Convert.ToSingle(doc.SelectSingleNode("root/prob_color").InnerText); entity.Prob_thin = Convert.ToSingle(doc.SelectSingleNode("root/prob_thin").InnerText); entity.Len_sma = Convert.ToInt32(doc.SelectSingleNode("root/len_sma").InnerText); entity.M_base = Convert.ToInt32(doc.SelectSingleNode("root/m_base").InnerText); entity.Thre_Area = Convert.ToInt32(doc.SelectSingleNode("root/thre_area").InnerText); entity.Thre_Location = Convert.ToInt32(doc.SelectSingleNode("root/thre_location").InnerText); entity.Index_shift = Convert.ToInt32(doc.SelectSingleNode("root/index_shift").InnerText); entity.Thresh_ratio = Convert.ToSingle(doc.SelectSingleNode("root/thresh_ratio").InnerText); entity.Num_ratio = Convert.ToSingle(doc.SelectSingleNode("root/num_ratio").InnerText); return entity; } catch (Exception ex) { LogisTrac.WriteLog(string.Format("GetConfig:{0}", ex.Message)); return null; } } } public class ParamEntity { //CLAS_PARAM public float Prob_cls { get; set; }//通用瑕疵 public float Prob_sma { get; set; }//小瑕疵 public float Prob_color { get; set; }//色纤 public float Prob_thin { get; set; }//淡痕 public int Len_sma { get; set; }//小瑕疵长度限制阈值 public int M_base { get; set; }//增益基准值 public int Thre_Area { get; set; }//点状瑕疵面积 public int Thre_Location { get; set; }//布尾米数位置阈值 //DETC_PARAM public int Index_shift { get; set; }//切边偏移 public float Thresh_ratio { get; set; }//二值化系数 public float Num_ratio { get; set; }//二值化系数2 } }