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 25using 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...

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

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

1 2 3 4 5 6 7 8 9 10 11using 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 24public 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; } } ...

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 24public 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); ...