Sunday, April 9, 2017

C# code snippet for setting up a FileSystemWatcher on a directory or file

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
using System;
using System.IO;

 FileSystemWatcher watcher = new FileSystemWatcher();
 watcher.Path = @"C:\filedumps";
        /* Watch for changes in LastWrite times, and the renaming of files  */
        watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName;
        // watch text files
        watcher.Filter = "*.txt";
        // Add event handlers
        watcher.Changed += new FileSystemEventHandler(OnChanged);
        watcher.Renamed += new RenamedEventHandler(OnRenamed);

        // raise events
        watcher.EnableRaisingEvents = true;

    private static void OnChanged(object source, FileSystemEventArgs e)
    {
          Console.WriteLine("File: " +  e.FullPath + " " + e.ChangeType);
    }

    private static void OnRenamed(object source, RenamedEventArgs e)
    {
          Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath);
    }

C# code snippet for creating a self hosted WCF service

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 ServiceHost employeeServiceHost = null;
            try
            {
                //Base Address for EmployeeService
                Uri httpBaseAddress = new Uri("http://localhost:8080/EmployeeService");
                
                //Instantiate ServiceHost
                employeeServiceHost = new ServiceHost(typeof(Employee.EmployeeService),httpBaseAddress); 
 
               //Add Endpoint to Host
                employeeServiceHost.AddServiceEndpoint(typeof(EmployeeService.IEmployeeService),new WSHttpBinding(), "");            
 
               //Metadata Exchange
                ServiceMetadataBehavior serviceBehavior = new ServiceMetadataBehavior();
                serviceBehavior.HttpGetEnabled = true;
                employeeServiceHost.Description.Behaviors.Add(serviceBehavior);
 
                //Open
                employeeServiceHost.Open();
                Console.WriteLine("Employee Service is listening at : {0}", httpBaseAddress);
                Console.ReadKey();                
            }
            catch (Exception ex)
            {
                employeeServiceHost = null;
                Console.WriteLine(" An error occurred with EmployeeService" + ex.Message);
            }

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);
  }