Scouser

Hello All

I am writing a small application that will constantly ping 25 or so devices on our network. Eventually it will email me if one doesn't ping, but for now I am wondering what is the best way to store the list of IP addresses

At the moment, I have a CSV file with 25 lines in like this:

1, Unit 20 New Front Door, 198.155.92.103,1

Meaning > [ID], [Description], Paradise, [online flag]

The basis of the program will loop through each IP address, pinging it (I'm ok with the ping code).

Whats the best way to go about this I'll need to allow the user to alter/add/remove IP addresses etc using an interface (not the CSV file itself) - perhaps display the values in a list box I'll need to change the [online flag] to "0" if it's offline, then not include it in the next ping run (so I dont get an email every 10 seconds).

I am using StreamReader at the moment..

Any advice is greatly appreciated - I am still relatively new to C#, and this is my first post - so go easy on me Smile

Regards

Maz



Re: Visual C# General Storing Application Settings and Values

Cliff Benoist

If this is a web app, you could use the web.config. If this is a winform app, you could use the [yourapp].exe.config file. These files allow you to store information like you want to store in XML format. You can then read the values out when your application starts up.

You could also store the values in a database and read them into the application. Using this option you could store information about when the devices are not pinging as you expect. Makes a nice audit trail to show someone.

Hope that was a bit helpful.





Re: Visual C# General Storing Application Settings and Values

Scouser

Thanks Cliff Smile

It's a winform app.....

I like the idea of using the config file (Is this different from using an XML file )

Also, would this allow me to change the values depending on ping result (I.e. change the flag to 0 if there is no ping )

If you could point me in the general direction of some coding that would be great - In the meantime, I will go away and look at how to store values in an XML file.....

Thanks again

Maz





Re: Visual C# General Storing Application Settings and Values

John Willemse

Funny how Paradise gets transformed to an icon Wink

Any ways, there are numerous ways to achieve what you want.

  • You can use an XML-file to store the settings, which is quite easy to learn and is especially suited for your application I think.
  • You can create a class and a collection to store the settings and serialize the class to a binary file on disk. This is probably the least code to write and also easy to learn. The file itself can not be edited outside your GUI though, since it's a binary representation of the class instances.
  • You can use a database, but since you have little data to store I would advice not to. Too much hassle and it may introduce bugs and exceptions you do not expect.
I personally prefer binary serialization because it's done without much effort. Here's some instructions and example code you could use to achieve it (from the top of my head, excuse any typos):


  1. Create a class that will store the information about the ping, like ID, description, IP addess, etc.:
    public class myPing
    {
    public myPing(string Description, string IP, bool Online)
    {
    _Description = Description;
    _IP = IP;
    _Online = Online;
    }

    private string _Description;
    public string Description
    {
    get { return _Description; }
    set { _Description = value; }
    }

    // Create all other properties...
    }
  2. Create another class that is a collection of you first class:
    public class myPingCollection : List<myPing>
    {
    public myPingsCollection() : base()
    {
    }
    }

    The collection class doesn't need any further code, it's simply a means of iterating through your ping settings.
  3. In your form's code you need to create methods to load and save these settings. These methods require some additional namespaces to be imported:
    using System.Runtime.Serialization;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.IO;
  4. Loading the settings, assuming you stored the settings in "c:\myPingSettings.dat" and you have a variable called myPingSettings which is of type myPingCollection:
    private void LoadPingSettings()
    {
    BinaryFormatter formatter = new BinaryFormatter();

    using (FileStream stream = new FileStream(@"c:\myPingSettings.dat", FileMode.Open, FileAccess.Read, FileShare.None))
    {
    myPingSettings = formatter.Deserialize(stream) As myPingCollection;
    }
    }
  5. Saving the settings:
    private void SavePingSettings()
    {
    BinaryFormatter formatter = new BinaryFormatter();

    using (FileStream stream = new FileStream(
    @"c:\myPingSettings.dat", FileMode.OpenOrCreate, FileAccess.Write, FileShare.None))
    {
    formatter.Serialize(stream, myPingSettings);
    }

    }
That's basically it. To change settings, you can access the collection by index and modify the properties:

myPingSettings[5].Description = "Backdoor 2";


Please note I didn't catch any exceptions that may occur.

I hope this helps a bit.

Best regards,
John Willemse





Re: Visual C# General Storing Application Settings and Values

In Love With Dotnet

Hi,

Yeah, there are many ways but the simple one would be resource file.

Its good to use resource (.resx) files which is easy to navigate through n easy to add/update/remove. This resource file is meant to store such relative data only.

Resources.NAMEOFRESOURCEFILE.ResourceManager.GetString("NAME")...........Simple

Hope this will help.






Re: Visual C# General Storing Application Settings and Values

boban.s

Xml file will be definitelly best in this situation. You can store this IP's in application settings also, and if you store only the ip's then you can store values separated but some character in application settings. But if you decide to additionaly store some other data connected with each ip then typed dataset will help you reading xml file and saving changes to xml file be as one line operation each.






Re: Visual C# General Storing Application Settings and Values

Scouser

Many thanks for the detailed responses - much appreciated

I'll give it all a try and report back (when I'm banging my head against the wall)

Cheers

Maz