////////////////////////////////////////////////////////////////////////////////
//
// SNMP Observer V 1.0.0.0 (2005-04-01 01:01:01)
//
// Copyright (c) 2005 Bashar A. Hamdan
// Author: Bashar A. Hamdan
//
////////////////////////////////////////////////////////////////////////////////
using System;
using System.Data;
using System.Globalization;
namespace SnmpObserver{
/// <summary>
/// MyConfig: This class encapsulates reading / writing to the config file.
/// </summary>
public class MyConfig
{
private int interval;
private int iterations;
private string community;
private int snmpTimeout;
private int snmpRetries;
public int Interval
{
set { this.interval = value; }
get { return this.interval; }
}
public int Iterations
{
set { this.iterations = value; }
get { return this.iterations; }
}
public int SnmpTimeout
{
set { this.snmpTimeout = value; }
get { return this.snmpTimeout; }
}
public int SnmpRetries
{
set { this.snmpRetries = value; }
get { return this.snmpRetries; }
}
public string Community
{
set { this.community = value.ToLower(); }
get { return this.community.ToLower(); }
}
//Constructor
public MyConfig()
{
// Load the config file on initialize
LoadData();
}
public void LoadData()
{
DataSet ds = new DataSet();
try
{
//Using the dataset. Its easy to save it as xml and load it back into the dataset.
ds.ReadXml("SnmpObserver.ini");
DataRow dr = ds.Tables[0].Rows[0];
double d = 0.0;
if (double.TryParse(dr["Interval"].ToString(), NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out d))
{
this.interval = int.Parse(dr["Interval"].ToString());
}
else
{
this.interval = 0;
}
d = 0.0;
if (double.TryParse(dr["Iterations"].ToString(), NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out d))
{
this.iterations = int.Parse(dr["Iterations"].ToString());
}
else
{
this.iterations = 0;
}
if (double.TryParse(dr["SnmpRetries"].ToString(), NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out d))
{
this.snmpRetries = int.Parse(dr["SnmpRetries"].ToString());
}
else
{
this.snmpRetries = 1;
}
if (double.TryParse(dr["SnmpTimeout"].ToString(), NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out d))
{
this.snmpTimeout = int.Parse(dr["SnmpTimeout"].ToString());
}
else
{
this.snmpTimeout = 30;
}
this.community = dr["Community"].ToString();
}
catch(Exception)
{
this.interval = 0;
this.snmpTimeout = 1;
this.snmpRetries = 1;
this.community = "Public";
this.iterations = 1;
}
}
public void SaveData()
{
// create a new dataset
DataSet ds = new DataSet("SnmpObserver");
DataColumn dc;
// create a table
DataTable dt = new DataTable("AppOptions");
DataRow dr = dt.NewRow();
//create columns
dc = new DataColumn("Community", System.Type.GetType("System.String"));
dt.Columns.Add(dc);
dc = new DataColumn("Iterations", System.Type.GetType("System.String"));
dt.Columns.Add(dc);
dc = new DataColumn("Interval", System.Type.GetType("System.String"));
dt.Columns.Add(dc);
dc = new DataColumn("SnmpTimeout", System.Type.GetType("System.String"));
dt.Columns.Add(dc);
dc = new DataColumn("SnmpRetries", System.Type.GetType("System.String"));
dt.Columns.Add(dc);
// load data into the dataset
dr["Community"] = this.community;
dr["Iterations"] = this.iterations;
dr["Interval"] = this.interval;
dr["SnmpTimeout"] = this.snmpTimeout;
dr["SnmpRetries"] = this.snmpRetries;
dt.Rows.Add(dr);
ds.Tables.Add(dt);
// write the dataset to the file
ds.WriteXml("SnmpObserver.ini");
}
}
}
|