Friday, May 1, 2015

asp.net Get Date Difference In Days

   public static Int32 GetDateDiffInDays(string sourceDate, string targetDate)
        {
            TimeSpan spanPassed;
            DateTime date1 = System.Convert.ToDateTime(sourceDate);
            DateTime date2 = System.Convert.ToDateTime(targetDate);
            spanPassed = date1.Subtract(date2);

            return spanPassed.Days;
        }

asp.net delete file

  public static void DeleteFile(string absoluteFilePath)
        {
         
            try
            {
                if (File.Exists(absoluteFilePath))
                {
                    File.Delete(absoluteFilePath);
                }
            }
            catch(Exception ex)
            {
                logger.LogError("Library.Functions.DeleteFile(string): Exception: " + ex.ToString());
            }
        }

asp.net Write To File

   public static void WriteToFile(string inputText, string absoluteFilePath)
        {
            try
            {
                FileStream fs = new FileStream(absoluteFilePath, FileMode.Create, FileAccess.Write);
                StreamWriter sw = new StreamWriter(fs);
                sw.Write(inputText);
                sw.Close();
                fs.Close();
            }
            catch { }
        }

asp.net get host details

 public static string GetHostDetails()
        {
            string hostName = GetHostName();
            string hostIP = GetHostIP();

            return hostName + " " + hostIP;
        }

asp.net using host name, get the IP address list

   public static string GetHostIP()
        {
            string hostName = GetHostName();
            string hostIP = "";

            // using host name, get the IP address list..
            IPHostEntry ipEntry = Dns.GetHostEntry(hostName);
            IPAddress[] ipAddress = ipEntry.AddressList;

            for (int i = 0; i < ipAddress.Length; i++)
            {
                hostIP = hostIP + ipAddress[i].ToString();
            }
            return hostIP;
        }

asp.net get the host name of local machine

 public static string GetHostName()
        {
            string hostName = "";
            // Getting Ip address of local machine...
            // First get the host name of local machine.
            hostName = Dns.GetHostName ();
           
            return hostName;
        }

asp.net get time

 public static string GetTime(string testTime, int timeFormat)
        {
            //string result = "";
            switch (timeFormat)
            {
                case 1:
                    return DateTime.Now.ToLongTimeString();
                case 2:
                    return (DateTime.Now).ToString("hh:mm:ss");
                default: return "";
            }
        }