Network Information Part 2.

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.NetworkInformation;
using System.Xml;

namespace NetworkInformation
{
class Program
{
static void Main(string[] args)
{
IPGlobalProperties props     = IPGlobalProperties.GetIPGlobalProperties();
NetworkInterface[] interfete = NetworkInterface.GetAllNetworkInterfaces();

//creez fisier XML
XmlTextWriter textWriter = new XmlTextWriter(@”C:\Documents and Settings\admin\Desktop\SD\networkInfo.xml”, null);

//deschid pentru a scrie
textWriter.WriteStartDocument();

//scriu un comentariu in fisierul XML
textWriter.WriteComment(“Interfete retea pentru ” + props.HostName);

//primul element
textWriter.WriteStartElement(“host”);

foreach (NetworkInterface interfata in interfete)
{
IPInterfaceProperties ipprop = interfata.GetIPProperties();

//begin interface
textWriter.WriteStartElement(“interface”);

//attribute
textWriter.WriteAttributeString(“name”, interfata.Name);

textWriter.WriteStartElement(“description”);
textWriter.WriteString(interfata.Description);
textWriter.WriteEndElement();

textWriter.WriteStartElement(“type”);
textWriter.WriteString(interfata.NetworkInterfaceType.ToString());
textWriter.WriteEndElement();

if (interfata.NetworkInterfaceType == NetworkInterfaceType.Loopback)
continue;

textWriter.WriteStartElement(“viteza”);
textWriter.WriteString(interfata.Speed + “bps”);
textWriter.WriteEndElement();

textWriter.WriteStartElement(“stare”);
textWriter.WriteString(interfata.OperationalStatus.ToString());
textWriter.WriteEndElement();

textWriter.WriteStartElement(“adresaFizica”);
textWriter.WriteString(interfata.GetPhysicalAddress().ToString());
textWriter.WriteEndElement();

textWriter.WriteStartElement(“sufixDNS”);
textWriter.WriteString(ipprop.DnsSuffix);
textWriter.WriteEndElement();

textWriter.WriteStartElement(“adreseIP”);
UnicastIPAddressInformationCollection adreseIP = ipprop.UnicastAddresses;
if (adreseIP.Count > 0)
{
foreach (UnicastIPAddressInformation uni in adreseIP)
{
textWriter.WriteAttributeString(“adresaIPunicast”,uni.Address.ToString());

textWriter.WriteStartElement(“Mask”);
textWriter.WriteString(uni.IPv4Mask.ToString());
textWriter.WriteEndElement();

/*Console.WriteLine((DateTime.UtcNow + TimeSpan.FromSeconds(uni.DhcpLeaseLifetime)).ToString());
Console.Read();
*/

/*
textWriter.WriteStartElement(“DHCP lease time”);
textWriter.WriteString((DateTime.UtcNow + TimeSpan.FromSeconds(uni.DhcpLeaseLifetime)).ToString());
textWriter.WriteEndElement();*/
}
}
textWriter.WriteEndElement();

//end interface
textWriter.WriteEndElement();
}
textWriter.WriteEndElement();

// end the document.
textWriter.WriteEndDocument();

// close writer
textWriter.Close();

}
}
}

Leave a Reply