Modernized xml reading and saving
This commit is contained in:
parent
2b195946e3
commit
c722d39eb1
2 changed files with 187 additions and 124 deletions
|
|
@ -480,15 +480,18 @@ public partial class MainForm
|
||||||
Text = "Image Catalog - " + LeggiSoloNomeFile(ilNome);
|
Text = "Image Catalog - " + LeggiSoloNomeFile(ilNome);
|
||||||
|
|
||||||
// 2021
|
// 2021
|
||||||
if (int.TryParse(_parametriSetup.LeggiParametroString("ChunkSize"), out var chunkSize))
|
Model.ChunkSize = _parametriSetup.LeggiParametro("ChunkSize", Model.ChunkSize);
|
||||||
{
|
Model.ThreadsCount = _parametriSetup.LeggiParametro("ThreadsCount", Model.ThreadsCount);
|
||||||
Model.ChunkSize = chunkSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (int.TryParse(_parametriSetup.LeggiParametroString("ThreadsCount"), out var threadsCount))
|
// if (int.TryParse(_parametriSetup.LeggiParametroString("ChunkSize"), out var chunkSize))
|
||||||
{
|
// {
|
||||||
Model.ThreadsCount = threadsCount;
|
// Model.ChunkSize = chunkSize;
|
||||||
}
|
// }
|
||||||
|
|
||||||
|
// if (int.TryParse(_parametriSetup.LeggiParametroString("ThreadsCount"), out var threadsCount))
|
||||||
|
// {
|
||||||
|
// Model.ThreadsCount = threadsCount;
|
||||||
|
// }
|
||||||
|
|
||||||
// Model.ChunkSize = int.Parse(_parametriSetup.LeggiParametroString("ChunkSize"));
|
// Model.ChunkSize = int.Parse(_parametriSetup.LeggiParametroString("ChunkSize"));
|
||||||
// Model.ThreadsCount = int.Parse(_parametriSetup.LeggiParametroString("ThreadsCount"));
|
// Model.ThreadsCount = int.Parse(_parametriSetup.LeggiParametroString("ThreadsCount"));
|
||||||
|
|
|
||||||
|
|
@ -1,179 +1,239 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Data;
|
using System.Data;
|
||||||
using Microsoft.VisualBasic;
|
|
||||||
|
|
||||||
namespace ImageCatalog
|
namespace ImageCatalog
|
||||||
{
|
{
|
||||||
public class ParametriSetup
|
public class ParametriSetup
|
||||||
{
|
{
|
||||||
private DataSet _ElencoParametri;
|
private DataSet _elencoParametri;
|
||||||
private string _NomeFileSetup;
|
public string NomeFileSetup { get; set; }
|
||||||
|
|
||||||
public ParametriSetup(string FileSetup)
|
public ParametriSetup(string fileSetup)
|
||||||
{
|
{
|
||||||
_ElencoParametri = new DataSet();
|
_elencoParametri = new DataSet();
|
||||||
_NomeFileSetup = FileSetup;
|
NomeFileSetup = fileSetup;
|
||||||
if (!string.IsNullOrEmpty(FileSetup))
|
if (!string.IsNullOrWhiteSpace(fileSetup))
|
||||||
{
|
{
|
||||||
CaricaParametriSetup();
|
CaricaParametriSetup();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public ParametriSetup()
|
public ParametriSetup() : this(string.Empty)
|
||||||
{
|
{
|
||||||
_ElencoParametri = new DataSet();
|
|
||||||
_NomeFileSetup = "";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void CaricaParametriSetup()
|
public void CaricaParametriSetup()
|
||||||
{
|
{
|
||||||
_ElencoParametri = LeggiXmlDataSet("Setup", _NomeFileSetup, "Nome");
|
if (string.IsNullOrEmpty(NomeFileSetup) || !File.Exists(NomeFileSetup))
|
||||||
|
{
|
||||||
|
_elencoParametri = new DataSet();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_elencoParametri = LeggiXmlDataSet("Setup", NomeFileSetup, "Nome");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SalvaParametriSetup()
|
public void SalvaParametriSetup()
|
||||||
{
|
{
|
||||||
if (System.IO.File.Exists(_NomeFileSetup) == true)
|
if (string.IsNullOrWhiteSpace(NomeFileSetup))
|
||||||
{
|
throw new InvalidOperationException("NomeFileSetup is not set.");
|
||||||
FileSystem.Kill(_NomeFileSetup);
|
|
||||||
}
|
|
||||||
|
|
||||||
_ElencoParametri.WriteXml(_NomeFileSetup);
|
// overwrite without FileSystem.Kill
|
||||||
|
_elencoParametri.WriteXml(NomeFileSetup, XmlWriteMode.WriteSchema);
|
||||||
}
|
}
|
||||||
|
|
||||||
public string LeggiParametroString(string NomeParametro)
|
// public string LeggiParametroString(string NomeParametro)
|
||||||
{
|
// {
|
||||||
string Risposta = "";
|
// string Risposta = "";
|
||||||
try
|
// try
|
||||||
{
|
// {
|
||||||
var LElenco = _ElencoParametri.Tables["Setup"].Select("Nome='" + NomeParametro + "'");
|
// var LElenco = _elencoParametri.Tables["Setup"].Select("Nome='" + NomeParametro + "'");
|
||||||
foreach (var LaRiga in LElenco)
|
// foreach (var LaRiga in LElenco)
|
||||||
Risposta = LaRiga["Valore"].ToString();
|
// Risposta = LaRiga["Valore"].ToString();
|
||||||
}
|
// }
|
||||||
catch
|
// catch
|
||||||
{
|
// {
|
||||||
Risposta = "";
|
// Risposta = "";
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
|
// return Risposta;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public bool LeggiParametroBoolean(string nomeParametro)
|
||||||
|
// {
|
||||||
|
// var risposta = "";
|
||||||
|
// try
|
||||||
|
// {
|
||||||
|
// var lElenco = _elencoParametri.Tables["Setup"].Select("Nome='" + nomeParametro + "'");
|
||||||
|
// foreach (var laRiga in lElenco)
|
||||||
|
// 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;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
return Risposta;
|
public string LeggiParametroString(string nomeParametro)
|
||||||
|
{
|
||||||
|
return GetRow(nomeParametro)?["Valore"]?.ToString() ?? string.Empty;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool LeggiParametroBoolean(string nomeParametro)
|
public bool LeggiParametroBoolean(string nomeParametro)
|
||||||
{
|
{
|
||||||
var risposta = "";
|
var raw = LeggiParametroString(nomeParametro);
|
||||||
|
return raw?.ToUpperInvariant() switch
|
||||||
|
{
|
||||||
|
"TRUE" or "OK" or "SI" or "1" or "YES" or "VERO" => true,
|
||||||
|
_ => false
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AggiornaParametro(string nomeParametro, object valoreParametro)
|
||||||
|
{
|
||||||
|
var table = EnsureSetupTable();
|
||||||
|
|
||||||
|
var rows = table.Select($"Nome='{nomeParametro.Replace("'", "''")}'");
|
||||||
|
if (rows.Length == 0)
|
||||||
|
{
|
||||||
|
var newRow = table.NewRow();
|
||||||
|
newRow["Nome"] = nomeParametro;
|
||||||
|
newRow["Valore"] = valoreParametro?.ToString() ?? string.Empty;
|
||||||
|
table.Rows.Add(newRow);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
rows[0]["Valore"] = valoreParametro?.ToString() ?? string.Empty;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public T LeggiParametro<T>(string nomeParametro, T defaultValue = default!)
|
||||||
|
{
|
||||||
|
var raw = LeggiParametroString(nomeParametro);
|
||||||
|
if (string.IsNullOrEmpty(raw)) return defaultValue;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var lElenco = _ElencoParametri.Tables["Setup"].Select("Nome='" + nomeParametro + "'");
|
return (T)Convert.ChangeType(raw, typeof(T));
|
||||||
foreach (var laRiga in lElenco)
|
|
||||||
risposta = laRiga["Valore"].ToString();
|
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
risposta = "";
|
return defaultValue;
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
private static DataSet LeggiXmlDataSet(string nomeTabella, string nomeFileXml, string nomeColonnaChiave = "")
|
||||||
{
|
{
|
||||||
try
|
var ds = new DataSet();
|
||||||
{
|
ds.ReadXml(nomeFileXml);
|
||||||
if (_ElencoParametri.Tables["Setup"] is null)
|
|
||||||
{
|
|
||||||
var TabellaTmp = new DataTable("Setup");
|
|
||||||
DataRow RigaTmp;
|
|
||||||
DataColumn LaColonna;
|
|
||||||
LaColonna = TabellaTmp.Columns.Add("Nome", Type.GetType("System.String"));
|
|
||||||
LaColonna = TabellaTmp.Columns.Add("Valore", Type.GetType("System.String"));
|
|
||||||
|
|
||||||
// * Aggiunge alla tabella tutte le righe
|
if (!string.IsNullOrEmpty(nomeColonnaChiave) && ds.Tables.Contains(nomeTabella))
|
||||||
RigaTmp = TabellaTmp.NewRow();
|
{
|
||||||
RigaTmp["Nome"] = NomeParametro;
|
var table = ds.Tables[nomeTabella];
|
||||||
RigaTmp["Valore"] = ValoreParametro;
|
if (table.Constraints[nomeColonnaChiave] == null)
|
||||||
TabellaTmp.Rows.Add(RigaTmp);
|
|
||||||
_ElencoParametri.Tables.Add(TabellaTmp);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
var LElenco = _ElencoParametri.Tables["Setup"].Select("Nome='" + NomeParametro + "'");
|
table.Constraints.Add(nomeColonnaChiave, table.Columns[nomeColonnaChiave], true);
|
||||||
if (LElenco.Length == 0)
|
|
||||||
{
|
|
||||||
DataRow LaRiga;
|
|
||||||
LaRiga = _ElencoParametri.Tables["Setup"].NewRow();
|
|
||||||
LaRiga["Nome"] = NomeParametro;
|
|
||||||
LaRiga["Valore"] = ValoreParametro;
|
|
||||||
_ElencoParametri.Tables["Setup"].Rows.Add(LaRiga);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
LElenco[0]["Valore"] = ValoreParametro;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch
|
|
||||||
{
|
return ds;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private DataTable LeggiXmlDataTable(string NomeTabella, string NomeFileXml, string NomeColonnaChiave = "")
|
private DataTable EnsureSetupTable()
|
||||||
{
|
{
|
||||||
// * Crea e Legge il dataset dal file xml
|
if (!_elencoParametri.Tables.Contains("Setup"))
|
||||||
var 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);
|
var table = new DataTable("Setup");
|
||||||
|
table.Columns.Add("Nome", typeof(string));
|
||||||
|
table.Columns.Add("Valore", typeof(string));
|
||||||
|
_elencoParametri.Tables.Add(table);
|
||||||
}
|
}
|
||||||
|
|
||||||
// * Restituisce la risposta
|
return _elencoParametri.Tables["Setup"];
|
||||||
return DataSetXml.Tables[NomeTabella];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static DataSet LeggiXmlDataSet(string NomeTabella, string NomeFileXml, string NomeColonnaChiave = "")
|
private DataRow? GetRow(string nomeParametro)
|
||||||
{
|
{
|
||||||
// * Crea e Legge il dataset dal file xml
|
if (!_elencoParametri.Tables.Contains("Setup")) return null;
|
||||||
var DataSetXml = new DataSet();
|
var rows = _elencoParametri.Tables["Setup"]
|
||||||
DataSetXml.ReadXml(NomeFileXml);
|
.Select($"Nome='{nomeParametro.Replace("'", "''")}'");
|
||||||
|
return rows.FirstOrDefault();
|
||||||
// * 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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// public void AggiornaParametro(string NomeParametro, object ValoreParametro)
|
||||||
|
// {
|
||||||
|
// try
|
||||||
|
// {
|
||||||
|
// if (_elencoParametri.Tables["Setup"] is null)
|
||||||
|
// {
|
||||||
|
// var TabellaTmp = new DataTable("Setup");
|
||||||
|
// DataRow RigaTmp;
|
||||||
|
// DataColumn LaColonna;
|
||||||
|
// LaColonna = TabellaTmp.Columns.Add("Nome", Type.GetType("System.String"));
|
||||||
|
// LaColonna = TabellaTmp.Columns.Add("Valore", 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
|
||||||
|
// {
|
||||||
|
// var LElenco = _elencoParametri.Tables["Setup"].Select("Nome='" + NomeParametro + "'");
|
||||||
|
// if (LElenco.Length == 0)
|
||||||
|
// {
|
||||||
|
// DataRow LaRiga;
|
||||||
|
// LaRiga = _elencoParametri.Tables["Setup"].NewRow();
|
||||||
|
// LaRiga["Nome"] = NomeParametro;
|
||||||
|
// LaRiga["Valore"] = ValoreParametro;
|
||||||
|
// _elencoParametri.Tables["Setup"].Rows.Add(LaRiga);
|
||||||
|
// }
|
||||||
|
// else
|
||||||
|
// {
|
||||||
|
// LElenco[0]["Valore"] = ValoreParametro;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// catch
|
||||||
|
// {
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// private static DataSet LeggiXmlDataSet(string NomeTabella, string NomeFileXml, string NomeColonnaChiave = "")
|
||||||
|
// {
|
||||||
|
// // * Crea e Legge il dataset dal file xml
|
||||||
|
// var 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;
|
||||||
|
// }
|
||||||
|
//}
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue