Click here to Skip to main content
12,354,122 members (68,143 online)
Click here to Skip to main content
Articles » Languages » C# » Applications » Downloads

Stats

286.3K views
6.8K downloads
109 bookmarked
Posted

.NET DUMeter clone

, 10 Feb 2003 BSD
A DUMeter clone, but with some better/different reporting features.
log.mdb
MyDUMeter.exe
MyDUMeter
MyDUMeter
app.ico
Log.xsx
MyDUMeter.csproj.user
MyDUMeter.suo
log.mdb
MyDUMeter.exe
app.ico
Log.xsx
MyDUMeter.csproj.user
MyDUMeter.suo
MyDUMeter.exe
app.ico
Log.xsx
MyDUMeter.csproj.user
MyDUMeter.suo
log.mdb
MyDUMeter.exe
app.ico
Log.xsx
MyDUMeter.csproj.user
MyDUMeter.suo
log.mdb
MyDUMeter.exe
app.ico
Log.xsx
MyDUMeter.csproj.user
MyDUMeter.suo
log.mdb
MyDUMeter.exe
app.ico
Log.xsx
MyDUMeter.csproj.user
MyDUMeter.suo
log.mdb
MyDUMeter.exe
app.ico
Log.xsx
MyDUMeter.csproj.user
MyDUMeter.suo
log.mdb
MyDUMeter.exe
app.ico
Log.xsx
MyDUMeter.csproj.user
MyDUMeter.suo
log.mdb
MyDUMeter.exe
app.ico
Log.xsx
MyDUMeter.csproj.user
MyDUMeter.suo
log.mdb
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

Share

About the Author

leppie
Software Developer
South Africa South Africa
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160621.1 | Last Updated 11 Feb 2003
Article Copyright 2003 by leppie
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid