Catalog/imagecatalog.backup/ParametriSetup.cs
2024-10-14 18:54:30 +02:00

179 lines
No EOL
5.4 KiB
C#

using System;
using System.Data;
using Microsoft.VisualBasic;
namespace ImageCatalog
{
public class ParametriSetup
{
private DataSet _ElencoParametri;
private string _NomeFileSetup;
public ParametriSetup(string FileSetup)
{
_ElencoParametri = new DataSet();
_NomeFileSetup = FileSetup;
if (!string.IsNullOrEmpty(FileSetup))
{
CaricaParametriSetup();
}
}
public ParametriSetup()
{
_ElencoParametri = new DataSet();
_NomeFileSetup = "";
}
public void CaricaParametriSetup()
{
_ElencoParametri = LeggiXmlDataSet("Setup", _NomeFileSetup, "Nome");
}
public void SalvaParametriSetup()
{
if (System.IO.File.Exists(_NomeFileSetup) == true)
{
FileSystem.Kill(_NomeFileSetup);
}
_ElencoParametri.WriteXml(_NomeFileSetup);
}
public string LeggiParametroString(string NomeParametro)
{
string Risposta = "";
try
{
var LElenco = _ElencoParametri.Tables["Setup"].Select("Nome='" + NomeParametro + "'");
foreach (var LaRiga in LElenco)
Risposta = LaRiga["Valore"].ToString();
}
catch
{
Risposta = "";
}
return Risposta;
}
public bool LeggiParametroBoolean(string NomeParametro)
{
string 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;
}
}
}
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 DataTable LeggiXmlDataTable(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.Tables[NomeTabella];
}
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;
}
public string NomeFileSetup
{
get
{
return _NomeFileSetup;
}
set
{
_NomeFileSetup = value;
}
}
}
}