using Microsoft.VisualBasic; using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.Diagnostics; using System.IO; public class XMLSettings { private DataSet _ElencoParametri; private string _NomeFileSetup; public XMLSettings(string FileSetup) { _ElencoParametri = new DataSet(); _NomeFileSetup = FileSetup; if (!string.IsNullOrEmpty(FileSetup)) { CaricaParametriSetup(); } } public XMLSettings() { _ElencoParametri = new DataSet(); _NomeFileSetup = ""; } public void CaricaParametriSetup() { _ElencoParametri = LeggiXmlDataSet("Setup", _NomeFileSetup, "Nome"); } public void SalvaParametriSetup() { if (System.IO.File.Exists(_NomeFileSetup) == true) { File.Delete(_NomeFileSetup); } _ElencoParametri.WriteXml(_NomeFileSetup); } public Dictionary getParametriDict() { CaricaParametriSetup(); Dictionary dictParam = new Dictionary(StringComparer.OrdinalIgnoreCase); //DataRow[] LElenco = _ElencoParametri.Tables["Setup"].Select("Nome='" + NomeParametro + "'"); //DataTable table = _ElencoParametri.Tables["Setup"]; foreach (DataRow row in _ElencoParametri.Tables["Setup"].Rows) { dictParam.Add(row["Nome"].ToString(), row["Valore"]); } return dictParam; } public string LeggiParametroString(string NomeParametro) { string Risposta = ""; try { DataRow[] LElenco = _ElencoParametri.Tables["Setup"].Select("Nome='" + NomeParametro + "'"); DataRow LaRiga = null; foreach (DataRow LaRiga_loopVariable in LElenco) { LaRiga = LaRiga_loopVariable; Risposta = LaRiga["Valore"].ToString(); } } catch { Risposta = ""; } return Risposta; } public bool LeggiParametroBoolean(string NomeParametro) { string Risposta = ""; try { DataRow[] LElenco = _ElencoParametri.Tables["Setup"].Select("Nome='" + NomeParametro + "'"); DataRow LaRiga = null; foreach (DataRow LaRiga_loopVariable in LElenco) { LaRiga = LaRiga_loopVariable; Risposta = LaRiga["Valore"].ToString(); } } catch { Risposta = ""; } switch (Risposta.ToUpper()) { case "TRUE": case "OK": case "SI": case "1": case "YES": case "VERO": return true; default: return false; } } public void AggiornaParametro(string NomeParametro, object ValoreParametro) { try { if (_ElencoParametri.Tables["Setup"] == null) { DataTable TabellaTmp = new DataTable("Setup"); DataRow RigaTmp = null; DataColumn LaColonna = null; LaColonna = TabellaTmp.Columns.Add("Nome", System.Type.GetType("System.String")); LaColonna = TabellaTmp.Columns.Add("Valore", System.Type.GetType("System.String")); //* Aggiunge alla tabella tutte le righe RigaTmp = TabellaTmp.NewRow(); RigaTmp["Nome"] = NomeParametro; RigaTmp["Valore"] = ValoreParametro; TabellaTmp.Rows.Add(RigaTmp); _ElencoParametri.Tables.Add(TabellaTmp); } else { DataRow[] LElenco = _ElencoParametri.Tables["Setup"].Select("Nome='" + NomeParametro + "'"); if (LElenco.Length == 0) { DataRow LaRiga = null; LaRiga = _ElencoParametri.Tables["Setup"].NewRow(); LaRiga["Nome"] = NomeParametro; LaRiga["Valore"] = ValoreParametro; _ElencoParametri.Tables["Setup"].Rows.Add(LaRiga); } else { LElenco[0]["Valore"] = ValoreParametro; } } } catch { } } private DataTable LeggiXmlDataTable(string NomeTabella, string NomeFileXml, string NomeColonnaChiave = "") { //* Crea e Legge il dataset dal file xml System.Data.DataSet DataSetXml = new System.Data.DataSet(); DataSetXml.ReadXml(NomeFileXml); //* Aggiunge il campo chiave if (!string.IsNullOrEmpty(NomeColonnaChiave)) { DataSetXml.Tables[NomeTabella].Constraints.Add(NomeColonnaChiave, DataSetXml.Tables[NomeTabella].Columns[NomeColonnaChiave], true); } //* Restituisce la risposta return DataSetXml.Tables[NomeTabella]; } private static DataSet LeggiXmlDataSet(string NomeTabella, string NomeFileXml, string NomeColonnaChiave = "") { //* Crea e Legge il dataset dal file xml DataSet DataSetXml = new DataSet(); DataSetXml.ReadXml(NomeFileXml); //* Aggiunge il campo chiave if (!string.IsNullOrEmpty(NomeColonnaChiave)) { DataSetXml.Tables[NomeTabella].Constraints.Add(NomeColonnaChiave, DataSetXml.Tables[NomeTabella].Columns[NomeColonnaChiave], true); } //* Restituisce la risposta return DataSetXml; } public string NomeFileSetup { get { return _NomeFileSetup; } set { _NomeFileSetup = value; } } }