Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » C# » Applications » Downloads
 
Add your own
alternative version

.NET DUMeter clone

, 10 Feb 2003
A DUMeter clone, but with some better/different reporting features.
mydumeter-1.0-bin.zip
mydumeter-1.0-src.zip
MyDUMeter
MyDUMeter
app.ico
Log.xsx
MyDUMeter.csproj.user
MyDUMeter.suo
mydumeter-1.1-bin.zip
mydumeter-1.1-src.zip
app.ico
Log.xsx
MyDUMeter.csproj.user
MyDUMeter.suo
mydumeter-1.2-bin.zip
mydumeter-1.2-src.zip
app.ico
Log.xsx
MyDUMeter.csproj.user
MyDUMeter.suo
log.mdb
mydumeter-1.3-bin.zip
mydumeter-1.3-src.zip
app.ico
Log.xsx
MyDUMeter.csproj.user
MyDUMeter.suo
log.mdb
mydumeter-1.4-bin.zip
mydumeter-1.4-src.zip
app.ico
Log.xsx
MyDUMeter.csproj.user
MyDUMeter.suo
log.mdb
mydumeter-1.5-bin.zip
mydumeter-1.5-src.zip
app.ico
Log.xsx
MyDUMeter.csproj.user
MyDUMeter.suo
log.mdb
mydumeter-1.6-bin.zip
mydumeter-1.6-src.zip
app.ico
Log.xsx
MyDUMeter.csproj.user
MyDUMeter.suo
log.mdb
mydumeter-1.7-bin.zip
mydumeter-1.7-src.zip
app.ico
Log.xsx
MyDUMeter.csproj.user
MyDUMeter.suo
log.mdb
mydumeter-1.8-bin.zip
mydumeter-1.8-src.zip
app.ico
Log.xsx
MyDUMeter.csproj.user
MyDUMeter.suo
log.mdb
mydumeter-18-src.zip
app.ico
Log.xsx
MyDUMeter.csproj.user
log.mdb
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

About the Author

leppie
Software Developer
South Africa South Africa
No Biography provided
Follow on   Twitter

| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 11 Feb 2003
Article Copyright 2003 by leppie
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid