Sunday, 17 November 2013

Find IP Address from C#

Get IP Address from C# of visited your on your website.

        public static string GetIPAddress()
        {
            try
            {
                string strHostName = "";
                strHostName = System.Net.Dns.GetHostName();
                IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);
                IPAddress[] addr = ipEntry.AddressList;
                return addr[addr.Length - 1].ToString();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return null;
            }
        }

Sunday, 13 October 2013

Dynamically Sitemap Generator in ASP.NET using C# with limited links

 protected void GenrateBtn_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            Sitemap sitemap = new Sitemap();
            string FilePath = @"C:\Documents and Settings\LandKhoj.com\Desktop\Stiemap.xls";
            dt = ImportFromExcel(FilePath);

            sitemap.Create(dt, "", "", "", Server.MapPath("~/sitemap"));
        }




using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;


namespace BusinessLogicLayer
{
    public class Sitemap
    {
        public void Create(DataTable dtUrl, string LastModified, string changefreq, string priorty, string path)
        {
            try
            {
                //DeleteFile(path);
                int Loop = 0, start = 0, end = 0, rem = 0, count=0;
                string sb=string.Empty;
                double TotalUrl = dtUrl.Rows.Count;
                for (int i = 0; i < TotalUrl; i++)
                    if (i % 5 == 0)
                        Loop++;
                int vv = (int)TotalUrl % 5;
                if (TotalUrl % 5 != 0 && TotalUrl>5)
                {
                    Loop--;
                    rem = (int)TotalUrl % 5;
                    end=4;
                }
                else if (TotalUrl % 5 == 0 && TotalUrl > 5)
                {
                    rem = 0;
                    end = 5;
                }
                else if (TotalUrl % 5 != 0 && TotalUrl < 5)
                {
                    rem = (int)TotalUrl;
                    end = rem;
                }
                for (int i = 0; i < Loop; i++)
                {
                    sb = string.Empty;
                    sb = Value(dtUrl, start, end, LastModified, changefreq, priorty);
                    start = start + 5;
                    end = end + 5;
                    count++;
                    if (i == 0)
                        File.AppendAllText(path + @"\sitemap.xml", sb.ToString());
                    else
                        File.AppendAllText(path + @"\sitemap" + (count - 1).ToString() + ".xml", sb.ToString());
                }
                if (rem != 0 && TotalUrl > 5)
                {
                    sb = string.Empty;
                    sb = Value(dtUrl, start, start + rem, LastModified, changefreq, priorty);
                    File.AppendAllText(path + @"\sitemap" + count.ToString() + ".xml", sb.ToString());
                }
            }

            catch (Exception ex)
            {
                throw ex;
            }
        }

        public string Value(DataTable dt, int Start, int End, string LastModified, string changefreq, string priorty)
        {
            double tot = dt.Rows.Count;
            StringBuilder sb = new StringBuilder();
            //<?xml version="1.0" encoding="UTF-8"?>
            //<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">

            sb.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            sb.Append("<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd\">");
            for (int i = Start; i < End; i++)
            {
                sb.Append("<url>");
                sb.Append("<loc>");
                sb.Append(dt.Rows[i][0].ToString());
                sb.Append("</loc>");
                sb.Append("<lastmod>");
                sb.Append(LastModified);
                sb.Append("</lastmod>");
                sb.Append("<changefreq>");
                sb.Append(changefreq);
                sb.Append("</changefreq>");
                sb.Append("<priority>");
                sb.Append(priorty);
                sb.Append("</priority>");
                sb.Append("</url>");
            }
            sb.Append("</urlset>");
            return sb.ToString();
        }

        public bool DeleteFile(string path)
        {
            try
            {
                //if (File.Exists(path + @"\sitemap.xml"))
                //    File.Delete(path + @"\sitemap.xml");

                string[] pat = Directory.GetFiles(path+@"\sitemap\");
                foreach (string p in pat)
                    if (File.Exists(p))
                        File.Delete(p.ToString());
                return true;
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
                return false;
            }
        }
    }
}

Sunday, 6 October 2013

Introducing ASP.NET

ASP.NET was developed in direct response to the problems that developers had with classic ASP. Since ASP is in such wide use, however, Microsoft ensured that ASP scripts execute without modification on a machine with the .NET Framework (the ASP engine, ASP.DLL, is not modified when installing the .NET Framework). Thus, IIS can house both ASP and ASP.NET scripts on the same machine.