using System;
using System.IO;
using System.Windows.Forms;
using System.Runtime.Serialization.Formatters.Soap;
namespace YariSoft.Utils{
public class ConfigFile
{
#region Local variables
private string _fileName = "";
protected object _configObject = null;
#endregion
#region Properties
public object ConfigObject
{
get{ return _configObject; }
set{ this._configObject = value; }
}
public string FileName
{
get{ return this._fileName; }
set{ this._fileName = value; }
}
#endregion
#region Constructor/Destructor
public ConfigFile()
{
}
#endregion
#region Public functions
public static bool SaveToFile( ref string FileName, object ObjectToSave, bool ShowDialog )
{
bool status = false;
try{
YariSoft.Utils.ConfigFile info = new YariSoft.Utils.ConfigFile();
info.ConfigObject = ObjectToSave;
info.FileName = FileName;
if( ShowDialog ){
status = info.SaveAs();
FileName = info.FileName;
} else {
status = info.Save();
}
}catch( Exception Exp ){
YariSoft.Utils.YMessages.Show( Exp.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error );
return false;
}
return status;
}
public static object LoadFromFile( ref string FileName, bool ShowDialog )
{
object result = null;
try{
YariSoft.Utils.ConfigFile info = new YariSoft.Utils.ConfigFile();
info.FileName = FileName;
bool status;
if( ShowDialog ){
status = info.OpenAs();
}else{
status = info.Open();
}
if( status ){
result = info.ConfigObject;
FileName = info.FileName;
}
}catch( Exception Exp ){
YariSoft.Utils.YMessages.Show( Exp.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error );
return null;
}
return result;
}
virtual public bool SaveAs ()
{
SaveFileDialog Dialog = new SaveFileDialog();
bool status = false;
Dialog.FileName = this._fileName;
if( Dialog.ShowDialog() == DialogResult.OK ){
this._fileName = Dialog.FileName;
status = this.Save();
}
Dialog.Dispose();
return status;
}
virtual public bool Save ()
{
if( this._configObject == null ){
YariSoft.Utils.YMessages.Show( "Object for save operation was not initialized!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error );
return false;
}
if( this._fileName == "" ){
return this.SaveAs();
}
Stream stream = null;
try {
stream = (Stream)File.Open( this._fileName, FileMode.Create, FileAccess.Write );
SoapFormatter formatter = new SoapFormatter();
formatter.Serialize( stream, this._configObject );
stream.Close();
}catch( Exception Exp ){
YariSoft.Utils.YMessages.Show( Exp.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error );
if( stream != null ){
stream.Close();
}
return false;
}
return true;
}
virtual public bool OpenAs()
{
return this.OpenAs("");
}
virtual public bool OpenAs( string FileExtFilter )
{
OpenFileDialog Dialog = new OpenFileDialog();
if( FileExtFilter != "" ){
Dialog.Filter = FileExtFilter;
}
bool status = false;
if( Dialog.ShowDialog() == DialogResult.OK ){
this._fileName = Dialog.FileName;
status = this.Open();
}
Dialog.Dispose();
return status;
}
virtual public bool Open()
{
if( this._fileName == "" ){
return false;
}
FileInfo info = new FileInfo( this._fileName );
if( ! info.Exists ){
return false;
}
Stream stream = null;
try{
stream = (Stream)File.Open( this._fileName, FileMode.Open, FileAccess.Read );
SoapFormatter formatter = new SoapFormatter ();
this._configObject = formatter.Deserialize( stream );
stream.Close();
}catch( Exception Exp ){
YariSoft.Utils.YMessages.Show( Exp.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error );
if( stream != null ){
stream.Close();
}
return false;
}
return true;
}
#endregion
}
}
|