|
public class Persist
{
private static string m_path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + System.IO.Path.DirectorySeparatorChar;
private static bool DeSerializeAnObjectTraditional(ref object thisObject, ref string xmlOfAnObject)
{
bool result = false;
if ((thisObject != null))
{
try
{
System.IO.StringReader read = new System.IO.StringReader(xmlOfAnObject);
System.Xml.XmlTextReader reader = new System.Xml.XmlTextReader(read);
try
{
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(thisObject.GetType());
thisObject = serializer.Deserialize(reader);
result = true;
}
catch (System.Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
finally
{
reader.Close();
read.Close();
read.Dispose();
}
}
catch (System.Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
}
return result;
}
private static bool SerializeAnObjectTraditional(ref object thisObject, ref string xmlOfAnObject)
{
bool result = false;
System.IO.MemoryStream stream = new System.IO.MemoryStream();
try
{
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(thisObject.GetType());
serializer.Serialize(stream, thisObject);
stream.Position = 0;
doc.Load(stream);
xmlOfAnObject = doc.InnerXml;
result = true;
}
catch (System.Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
result = false;
}
finally
{
stream.Close();
stream.Dispose();
}
return result;
}
private static string SerializeAnObject(ref object thisObject)
{
string result = "";
if (typeof(System.Windows.Forms.CheckedListBox).Equals(thisObject.GetType()))
{
System.Windows.Forms.CheckedListBox x = (System.Windows.Forms.CheckedListBox)thisObject;
if (x.Items.Count > 0)
{
for (int i = 0; i <= x.Items.Count - 1; i++)
{
if (typeof(string).Equals(x.Items[i].GetType()))
{
if (x.CheckedIndices.Contains(i))
{
result += "1," + System.Convert.ToString(x.Items[i]) + System.Text.Encoding.ASCII.GetString(new byte[]{13,10});
}
else
{
result += "0," + System.Convert.ToString(x.Items[i]) + System.Text.Encoding.ASCII.GetString(new byte[]{13,10});
}
}
}
}
}
else if (typeof(System.Windows.Forms.ListBox).Equals(thisObject.GetType()))
{
System.Windows.Forms.ListBox x = (System.Windows.Forms.ListBox)thisObject;
if (x.Items.Count > 0)
{
for (int i = 0; i <= x.Items.Count - 1; i++)
{
if (typeof(string).Equals(x.Items[i].GetType()))
{
result += System.Convert.ToString(x.Items[i]) + System.Text.Encoding.ASCII.GetString(new byte[]{13,10});
}
}
}
}
else if (!SerializeAnObjectTraditional(ref thisObject, ref result))
{
//not supported apparently
}
if (result.EndsWith(System.Text.Encoding.ASCII.GetString(new byte[] { 13, 10 })))
result = result.Substring(0, result.Length - 2);
return result;
}
private static void DeSerializeAnObject(ref object thisObject, string xmlOfAnObject)
{
if (((thisObject != null)))
{
if (typeof(System.Windows.Forms.CheckedListBox).Equals(thisObject.GetType()))
{
System.Windows.Forms.CheckedListBox x = (System.Windows.Forms.CheckedListBox)thisObject;
string[] s = System.Text.RegularExpressions.Regex.Split(xmlOfAnObject, "\r\n");
for (int i = 0; i <= s.Length - 1; i++)
{
if (s[i].Length > 1 && s[i][1] == ',')
{
bool @checked = s[i][0] == '1';
s[i] = s[i].Remove(0, 2).Trim();
if (s[i].Length > 0)
{
if (!x.Items.Contains(s[i]))
{
x.Items.Add(s[i]);
}
x.SetItemChecked(x.Items.IndexOf(s[i]), @checked);
}
}
}
}
else if (typeof(System.Windows.Forms.ListBox).Equals(thisObject.GetType()))
{
System.Windows.Forms.ListBox x = (System.Windows.Forms.ListBox)thisObject;
string[] s = System.Text.RegularExpressions.Regex.Split(xmlOfAnObject, "\r\n");
for (int i = 0; i <= s.Length - 1; i++)
{
if (!x.Items.Contains(s[i]))
{
x.Items.Add(s[i]);
}
}
}
else if (!DeSerializeAnObjectTraditional(ref thisObject, ref xmlOfAnObject))
{
//not supported apparently
}
}
}
private static byte[] LoadBufferFromFile(string filename)
{
byte[] result = new byte[1];
try
{
if (System.IO.File.Exists(filename))
{
System.IO.FileStream fstream = new System.IO.FileStream(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);
byte[] fbuffer = new byte[System.Convert.ToInt32(fstream.Length - 1) + 1];
fstream.Read(fbuffer, 0, fbuffer.Length);
fstream.Close();
result = fbuffer;
}
}
catch (System.Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
return result;
}
private static string LoadFromFile(string filename)
{
return System.Text.ASCIIEncoding.ASCII.GetString(LoadBufferFromFile(filename));
}
public static void Load(string filename, object thisObject)
{
DeSerializeAnObject(ref thisObject, LoadFromFile(m_path + filename + ".ini"));
}
private static void SaveToFile(string filename, byte[] content)
{
try
{
System.IO.FileStream fstream = new System.IO.FileStream(filename, System.IO.FileMode.Create);
fstream.Write(content, 0, content.Length);
fstream.Close();
}
catch (System.Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
}
private static void SaveToFile(string filename, string content)
{
SaveToFile(filename, System.Text.ASCIIEncoding.ASCII.GetBytes(content));
}
public static void Save(string filename, object thisObject)
{
SaveToFile(m_path + filename + ".ini", SerializeAnObject(ref thisObject));
}
}
public class MachineID
{
//""
private static string m_ID = System.Guid.NewGuid().ToString();
public static string ID
{
get { return m_ID; }
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.