using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace Erfan
{
    public class RSS
    {
        public string title;
        public string description;
        public string link;

        public override string ToString()
        {
            return title;
        }
    }
    public class Net
    {
        [System.Runtime.InteropServices.DllImport("wininet.dll")]
        private extern static bool InternetGetConnectedState(out int Description, int ReservedValue);

        public static bool IsConnectedToInternet()
        {
            int Desc;
            return InternetGetConnectedState(out Desc, 0);
        }
        //----------------------------------------------------------------------------
        public static string URL2IP(string url)
        {
            try
            {
                IPHostEntry urlInfo = Dns.GetHostEntry(url);
                return urlInfo.AddressList[0].ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error in converting : " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return string.Empty;
            }
        }
        //----------------------------------------------------------------------------
        public static string Ping(string ip)
        {
            Ping ping = new Ping();
            PingReply pingReply = ping.Send(ip);

            string result;
            result = "Address: " + pingReply.Address;
            result += "\r\nTime in milliseconds: " + pingReply.RoundtripTime;
            result += "\r\nStatus: " + pingReply.Status.ToString();
            return result;
        }
        //----------------------------------------------------------------------------
        public static void RSS_Reader(string url, ListBox listBox)
        {
            try
            {
                listBox.Items.Clear();
                WebRequest webRequest = WebRequest.Create(url);
                Stream stream = webRequest.GetResponse().GetResponseStream();
                XmlDocument xmlDocument = new XmlDocument();
                xmlDocument.Load(stream);
                XmlNodeList items = xmlDocument.SelectNodes("rss/channel/item");

                foreach (XmlNode item in items)
                {
                    RSS rss = new RSS();
                    rss.title = item.SelectSingleNode("title").InnerText;
                    rss.description = item.SelectSingleNode("description").InnerText;
                    rss.link = item.SelectSingleNode("link").InnerText;
                    //----------------------------------------------------------------
                    listBox.Items.Add(rss);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}