Sunday, April 9, 2017

C# code snippet for creating, extracting a compressed zip file

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
using System;
using System.IO;
using System.IO.Compression;
     
           string filePath = @"c:\temp\EmployeeInfo";
            string zipPath = @"c:\temp\EmployeeInfo.zip";
            string extractPath = @"c:\temp\extract";

            ZipFile.CreateFromDirectory(startPath, zipPath);

            ZipFile.ExtractToDirectory(zipPath, extractPath);

C# program to generate prime numbers from 1 to n

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public static void IsPrime(int n)
  {

   foreach (var num in Enumerable.Range(2, n-1))
   {
    if (num == 2)
    {
     Console.WriteLine(num);
     continue;
    }
    var result = true;
    for (int i = 2; i <= (int)Math.Sqrt(num); i++)
    {
     if (num % i == 0)
     {
      result = false;
      break;
     }
    }
    if (result)
     Console.WriteLine(num);
   }
   
  }

C# program to test a number is prime or not

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public static void IsPrime(int number)
  {

   if (number == 1 || number == 0)
   {
    Console.WriteLine(false);
    return;
   }

   if (number == 2)
   {
    Console.WriteLine(true);
    return;
   }
   for (int i = 2; i <= (int)Math.Sqrt(number); i++)
   {
    if (number % i == 0)
    {
     Console.WriteLine(false);
     return;
    }
   }
   Console.WriteLine(true);
  }

Friday, November 4, 2016

Common ASP.Net Web Virtual and Relative Url helper classes

List of ASP.Net Web/MVC Virtual Paths,Relative and Absolute Paths Utility helper classes
Property Input Url Return Value
HttpRequest.ApplicationPath http://localhost/testapp/home.aspx /testapp
HttpRequest.CurrentExecutionFilePath http://localhost/testapp/home.aspx /testapp/home.aspx
HttpRequest.FilePath http://localhost/testapp/home.aspx /testapp/home.aspx
HttpRequest.AppRelativeCurrentExecutionFilePath http://localhost/testapp/home.aspx ~/testapp/home.aspx
HttpRequest.Path http://localhost/testapp/home.aspx /testapp/home.aspx/PathInfo
HttpRequest.PhysicalApplicationPath http://localhost/testapp/home.aspx C:\inetpub\wwwroot\TestApp
HttpRequest.RawUrl http://localhost/testapp/home.aspx?user=1 /testapp/home.aspx?user=1
UrlHelper.Content ~/testapp/home.aspx c:\inetpub\wwwroot\testapp\home.aspx
Property Input Url Return Value
HttpContext.Current.Request.Url.Host http://localhost:8080/testapp/home.aspx?user=1&new=2 localhost
HttpContext.Current.Request.Url.Authority http://localhost:8080/testapp/home.aspx?user=1&new=2 localhost:8080
HttpContext.Current.Request.Url.AbsolutePath http://localhost:8080/testapp/home.aspx?user=1&new=2 /testapp/home.aspx
HttpContext.Current.Request.Url.AbsoluteUri http://localhost:8080/testapp/home.aspx?user=1&new=2 http://localhost:8080/testapp/home.aspx?user=1&new=2
HttpContext.Current.Request.Url.PathAndQuery http://localhost:8080/testapp/home.aspx?user=1&new=2 /testapp/home.aspx?user=1&new=2
HttpContext.Current.Request.RawUrl http://localhost:8080/testapp/home.aspx?user=1&new=2 /testapp/home.aspx?user=1&new=2
Property Input Url Return Value
VirtualPathUtility.GetFileName http://localhost/testapp/home.aspx home
VirtualPathUtility.GetExtension http://localhost/testapp/home.aspx aspx
VirtualPathUtility.GetDirectory http://localhost/testapp/home.aspx /testapp/
VirtualPathUtility.IsAbsolute http://localhost/testapp/home.aspx True
VirtualPathUtility.IsAppRelative http://localhost/testapp/home.aspx False
VirtualPathUtility.ToAppRelative http://localhost/testapp/home.aspx ~/home.aspx
VirtualPathUtility.ToAbsolute ~/home.aspx /testapp/home
VirtualPathUtility.AppendTrailingSlash http://localhost/testapp/home.aspx http://localhost/testapp/home.aspx/