Click here to Skip to main content
15,886,873 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 344.2K   7.4K   111  
A DUMeter clone, but with some better/different reporting features.
using System;
using System.Globalization;
using System.Collections;
using System.Diagnostics;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace MyDUMeter
{
   public enum LineSpeed 
   {
      Modem28800 = 28800,
      Modem33600 = 33600,
      Modem56k = 56000,
      ISDN64k = 64000,
      ISDN128k = 128000,
      DSL512k = 512000,
      DSL1_5m = 1024000 + DSL512k,
      LAN10m = 10 * 1024000,
      LAN100m = 100 * 1024000,
   }

   [DefaultProperty("Interface")]
   public class ControlOptions
   {
      public ControlOptions()
      {
      }

      private LineSpeed speed = LineSpeed.ISDN128k; 

      [Category("Interface Options")]
      public LineSpeed LineSpeed 
      {
         get {return speed;}
         set {speed = value;}
      }

      private Color downcolor = Color.Red;
      private Color upcolor = Color.Green;
      private Color bothcolor = Color.Yellow;
      private Color textcolor = Color.Black;
      private Color bc = SystemColors.Control;
      private Color lines = Color.DimGray;
      private Color selcolor = Color.Blue;

      [Category("Color Options")]
      public Color Lines
      {
         get {return lines;}
         set {lines = value;}
      }

      [Category("Color Options")]
      public Color Selection
      {
         get {return selcolor;}
         set {selcolor = value;}
      }

      [Category("Color Options")]
      public Color Background
      {
         get {return bc;}
         set {bc = value;}
      }

      [Category("Color Options")]
      public Color Text 
      {
         get {return textcolor;}
         set {textcolor = value;}
      }

      [Category("Color Options")]
      public Color Down 
      {
         get {return downcolor;}
         set {downcolor = value;}
      }

      [Category("Color Options")]
      public Color Up 
      {
         get {return upcolor;}
         set {upcolor = value;}
      }

      [Category("Color Options")]
      public Color Both 
      {
         get {return bothcolor;}
         set {bothcolor = value;}
      }

      private string instancename = "";

      [Description("Please select an interface to montior")]
      [Category("Interface Options")]
      [TypeConverter(typeof(ControlOptions.InstanceConverter))]
      public string Interface 
      {
         get {return instancename;}
         set {instancename = value;}
      }
      
      class InstanceConverter : TypeConverter
      {
         public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
         {
            ArrayList vals = new ArrayList();
            string[] instnames = new PerformanceCounterCategory("Network Interface", sname).GetInstanceNames();
            foreach (string i in instnames)
               vals.Add(i);
            return new StandardValuesCollection(vals);
         }

         public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
         {
            return true;
         }
      }

      internal static string sname = ".";

      private string pcname = ".";

      [Category("Interface Options")]
      [Description("Select the PC you would like to monitor")]
      [TypeConverter(typeof(ControlOptions.MachineConvertor))]
      public string MachineName 
      {
         get {return pcname;}
         set 
         {
            pcname = value;
            sname = value;
         }
      }

      [DllImport("Netapi32")]
      private static extern int NetServerEnum(
         string servername, //must be null
         int level, //101
         out IntPtr buffer,
         int maxlen, //out
         out int entriesread, //out
         out int totalentries, //out
         int servertype, //in 3 = all
         string domain, //null primary
         int resumehandle); //must be 0

      [DllImport("Netapi32")]
      private static extern int NetApiBufferFree(IntPtr ptr);

      [StructLayout(LayoutKind.Sequential)]
      internal class ServerInfo 
      {
         public int platformid;
         [MarshalAs(UnmanagedType.LPWStr)]
         public string name;
         public int majorver;
         public int minorver;
         public int type;
         [MarshalAs(UnmanagedType.LPWStr)]
         public string comment;
      }

      class MachineConvertor : TypeConverter 
      {
         public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
         {
            int eread, etot;
            IntPtr buffer;
            int l = 20;     
            int size = Marshal.SizeOf(typeof(ServerInfo));
            int res = NetServerEnum(
               null, 
               101, 
               out buffer, 
               size * l, 
               out eread, 
               out etot, 
               3, 
               null, 
               0);
            
            IntPtr p = buffer;
            string[] arr = new string[etot];
            for (int i = 0; i < eread; i++)
            {
               ServerInfo si = Marshal.PtrToStructure(p, typeof(ServerInfo)) as ServerInfo;   
               if (si.majorver > 4)
                  arr[i] = si.name;
               p = (IntPtr)((int)p + size);
            }
      
            res = NetApiBufferFree(buffer);
            return new StandardValuesCollection(arr);
         }

         public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
         {
            return true;
         }
      }

      private double opac = 0.7F;

      [Category("Visual Options")]
      [TypeConverter(typeof(System.Windows.Forms.OpacityConverter))]
      public double Transparency 
      {
         get {return opac;}
         set {opac = value;}
      }

      private double overflow = 0.2F;

      [Category("Visual Options")]
      [TypeConverter(typeof(System.Windows.Forms.OpacityConverter))]
      public double Overflow 
      {
         get {return overflow;}
         set {overflow = value;}
      }

      private Point loc = new Point(0,0);

      [Browsable(false)]
      public Point Location 
      {
         get {return loc;}
         set {loc = value;}
      }

      private Size size = new Size(180,50);

      [Browsable(false)]
      public Size Size 
      {
         get {return size;}
         set {size = value;}
      }

      private bool loadwin = false;
      private bool checkreg = false;

      [Category("Windows Options")]
      public bool LoadWithWindows 
      {
         get 
         {
            if (!checkreg)
            {
               if (Microsoft.Win32.Registry.CurrentUser.OpenSubKey(
                  @"Software\Microsoft\Windows\CurrentVersion\Run").GetValue(
                  "MyDUMeter") != null)
                  loadwin = true;
               checkreg = true;
            }

            return loadwin;
         }
         set 
         {
            if (value)
               Microsoft.Win32.Registry.CurrentUser.OpenSubKey(
                  @"Software\Microsoft\Windows\CurrentVersion\Run", true).SetValue(
                  "MyDUMeter", Application.ExecutablePath);
            else 
               Microsoft.Win32.Registry.CurrentUser.OpenSubKey(
                  @"Software\Microsoft\Windows\CurrentVersion\Run", true).DeleteValue(
                  "MyDUMeter", false);
            loadwin = value;
         }
      }

      private bool alwaysshowtext = true;

      [Category("Visual Options")]
      public bool AlwaysShowText 
      {
         get {return alwaysshowtext;}
         set {alwaysshowtext = value;}
      }

      private string ping = "www.bbnplanet.net";

      [Category("Ping Options")]
      public string PingEndPoint 
      {
         get {return ping;}
         set {ping = value;}
      }

      private int pinginterval = 500;

      [Description("Period in millisenconds")]
      [Category("Ping Options")]
      public int PingInterval 
      {
         get {return pinginterval;}
         set {pinginterval = value;}
      }

      private bool hidewhenidle = false;

      [Category("Visual Options")]
      public bool HideWhenIdle 
      {
         get {return hidewhenidle;}
         set {hidewhenidle = value;}
      }

      private int loginterval = 60;

      [
      ReadOnly(true),
         Description("Log interval in seconds"),
         Category("Log Options")]
      public int LogInterval 
      {
         get {return loginterval;}
         set {loginterval = value;}
      }
   }
}

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