Torna al Thread
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;
using System.Drawing;
using System.IO;
namespace RMLSkeduler
{
/// <summary>
/// Esempio di serializzazione
/// Definizione una classe "Serializza"
/// Si decora la classe l'attributo [Serializable] (obbligatorio per i nostri scopi)
/// </summary>
[Serializable]
public class ClassSerializeLog
{
// private string mDirectorysalvataggioLog = @"C:\Documents and Settings\Proprietario\RML\Log.rmll";
// definizione membri privati
#region dati del log
private DateTime m_TimingSer;
public DateTime TimingSer
{
get { return m_TimingSer; }
set { m_TimingSer = value; }
}
private Utente m_UtenteLogSer;
public Utente UtenteLogSer
{
get { return m_UtenteLogSer; }
set { m_UtenteLogSer = value; }
}
private String m_AzioneLogSer;
public String AzioneLogSer
{
get { return m_AzioneLogSer; }
set { m_AzioneLogSer = value; }
}
private List<Logg> m_listaLoggSer = new List<Logg>();
public List<Logg> listaLoggSer
{
get { return m_listaLoggSer; }
set { m_listaLoggSer = value; }
}
#endregion dati del log
public ClassSerializeLog()
{
}
#region Metodi serializzazione
/// <summary>
/// Serializzazione della classe Serializza
/// </summary>
/// <param name="serData">Classe da serializzare</param>
/// <param name="NomeFile">Nome del file da generare</param=alla directory di installazione del RORX>
/// <returns>Operazione Eseguita/Non eseguita</returns>
public bool SerializzazioneLog(ClassSerializeLog serData, string NomeFile)
{
// NomeFile = mDirectorysalvataggioLog;
//salva un file di estensione .rmlx
bool RetValue = true;
MemoryStream ms = new MemoryStream();
BinaryFormatter br = new BinaryFormatter();
using (FileStream fs = new FileStream(NomeFile, FileMode.OpenOrCreate))
{
try
{
// il metodo Serialize di BinaryFormatter
// restituisce nella variabile ms (MemoryStream)
// la classe serializzata
br.Serialize(ms,serData);
// scriviamo sul file (FileStream fs)
ms.WriteTo(fs);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
System.Diagnostics.Debug.WriteLine(ex.StackTrace);
RetValue = false;
}
finally
{
fs.Close();
Console.WriteLine("le modifiche ai log sono state salvate nel file UtensLog.rmll");
}
}
return RetValue;
}
/// <summary>
/// Deserializzazione per la classe Serializza
/// </summary>
/// <param name="NomeFile"></param=è sempre la directory di installazione del RORX>
/// <returns></returns>
public ClassSerializeLog DeSerializzazioneLog(string NomeFile)
{
// NomeFile = mDirectorysalvataggioLog;
using (FileStream fs = new FileStream(NomeFile, FileMode.Open))
{
BinaryFormatter bf = new BinaryFormatter();
try
{
fs.Seek(0, SeekOrigin.Begin);
return (ClassSerializeLog)bf.Deserialize(fs);
}
catch
{
return null;
}
finally
{
fs.Close();
}
}
}
#endregion
}
}