Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / C#

.NET DUMeter clone

Rate me:
Please Sign up or sign in to vote.
4.86/5 (47 votes)
10 Feb 2003BSD4 min read 343.8K   7.4K   111  
A DUMeter clone, but with some better/different reporting features.
using System;
using System.Reflection;
using System.Xml;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace MyDUMeter
{
   public class Options
   {
      private Options(){}

      public static void Save(string filename, object options)
      {

         Byte[] buffer = new Byte[80];
         MemoryStream ms;
         BinaryFormatter bf = new BinaryFormatter();

         System.Xml.XmlTextWriter xmlwriter = 
            new XmlTextWriter(filename, System.Text.Encoding.Default);

         xmlwriter.Formatting = Formatting.Indented;
         xmlwriter.WriteStartDocument();

         xmlwriter.WriteComment("Option File. Do not edit! (c)llewellyn@pritchard.org");
         xmlwriter.WriteStartElement(options.ToString());
		
         PropertyInfo[] props = options.GetType().GetProperties(
            BindingFlags.Public | 
            BindingFlags.Instance | 
            BindingFlags.SetField);

         foreach (PropertyInfo prop in props)
         {
            xmlwriter.WriteStartElement(prop.Name);

            object da = prop.GetValue(options, null);

            if (da != null) 
            {
               xmlwriter.WriteAttributeString("Value", da.ToString());

               ms = new MemoryStream();
               try 
               {
                  bf.Serialize(ms, da);
                  ms.Position = 0;
                  int count = 0;
                  do 
                  {
                     count = ms.Read(buffer, 0, buffer.Length);
                     xmlwriter.WriteBase64(buffer, 0, count);
                  }
                  while ( count == buffer.Length);
               } 
               catch (System.Runtime.Serialization.SerializationException)
               {
                  Console.WriteLine("SERIALIZATION FAILED: {0}", prop.Name);
               }

            }
            else xmlwriter.WriteAttributeString("Value", "null");

            xmlwriter.WriteEndElement();
         }
         xmlwriter.WriteEndElement();
         xmlwriter.WriteEndDocument();
         xmlwriter.Flush();
         xmlwriter.Close();
      }

      public static void Load(string filename, object options)
      {
         Byte[] buffer = new Byte[80];
         MemoryStream ms;
         BinaryFormatter bf = new BinaryFormatter();

         System.Xml.XmlTextReader reader = new XmlTextReader(filename);

         while (reader.Read())
         {
            switch (reader.NodeType)
            {
               case XmlNodeType.Element:

                  if (reader.HasAttributes)
                  {
                     string name = reader.Name;
                     string val = reader.GetAttribute("Value");

                     ms = new MemoryStream();
		
                     int count = 0;
                     do 
                     {
                        count = reader.ReadBase64(buffer, 0 , buffer.Length);
                        ms.Write(buffer, 0,count);
                     }
                     while (count == buffer.Length);

                     ms.Position = 0;

                     if (val != "null") 
                     {
                        try 
                        {
                           object da = bf.Deserialize(ms);

                           Console.Write("Applying {0} : ", name);
                           options.GetType().GetProperty(name).SetValue(options, da, null);
                           Console.WriteLine("OK");
                        }
                        catch (System.Runtime.Serialization.SerializationException e)
                        {
                           Console.WriteLine("FAIL: {0}",e.Message);
                        }
                     }
                  }
                  break;
            }
         }
         reader.Close();
      }
   }
}

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.

License

This article, along with any associated source code and files, is licensed under The BSD License


Written By
Software Developer
South Africa South Africa
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions