Ans Home Assignment
Ans Home Assignment
using System; using System.IO; namespace Client.Chapter_11___File_and_Streams { public class MovingAFiles { static void Main(string[] args) { FileInfo MyFile = new FileInfo(@"c:\Projects\Testing.txt"); MyFile.Create(); MyFile.MoveTo(@"c:\Projects\MyFolder\Moved Testing.txt"); MyFile.MoveTo(@"C:|projects\MyFolder"); } } } Assignment 3 sing System; using System.IO; static class MainClass { static void Main(string[] args) { DirectoryInfo dir = new DirectoryInfo("c:\\a"); Console.WriteLine("Total size: " + CalculateDirectorySize(dir, true). ToString() + " bytes."); }
Assignment 4 using System; using System.IO; class MainClass { static void Main(string[] args) { FileInfo file = new FileInfo("c:\\a.txt"); // Display drive information. DriveInfo drv = new DriveInfo(file.FullName); Console.Write("Drive: "); Console.WriteLine(drv.Name); if (drv.IsReady) { Console.Write("Drive free space: ");
Console.WriteLine(drv.AvailableFreeSpace.ToString()); } } } Assignment 5 public void OnRenamed(object source, RenamedEventArgs e) { try { StreamWriter sw = new StreamWriter("C:/Log.txt", true); sw.WriteLine("File renamed from {0} to {1}", e.OldName,e.Full Path); sw.Close(); this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText) ,"Wrote renamed event to log"); }
Assignment 6
using System; class Overload { public void ovlDemo() { Console.WriteLine("No parameters"); } // Overload ovlDemo for one integer parameter. public void ovlDemo(int a) { Console.WriteLine("One parameter: " + a); } // Overload ovlDemo for two integer parameters. public int ovlDemo(int a, int b) { Console.WriteLine("Two parameters: " + a + " " + b); return a + b; } // Overload ovlDemo for two double parameters. public double ovlDemo(double a, double b) { Console.WriteLine("Two double parameters: " + a + " "+ b); return a + b; } } class MainClass { public static void Main() { Overload ob = new Overload(); int resI; double resD; // call all versions of ovlDemo() ob.ovlDemo(); Console.WriteLine(); ob.ovlDemo(2);
Console.WriteLine(); resI = ob.ovlDemo(4, 6); Console.WriteLine("Result of ob.ovlDemo(4, 6): " + resI); Console.WriteLine(); resD = ob.ovlDemo(1.1, 2.32); Console.WriteLine("Result of ob.ovlDemo(1.1, 2.32): " + resD); } }
Assignment 7
using System; class MyClass { public int x; public MyClass() { Console.WriteLine("Inside MyClass()."); x = 0; } public MyClass(int i) { Console.WriteLine("Inside MyClass(int)."); x = i; } public MyClass(double d) { Console.WriteLine("Inside MyClass(double)."); x = (int) d; } public MyClass(int i, int j) { Console.WriteLine("Inside MyClass(int, int)."); x = i * j; } } class MainClass { public static void MyClass t1 = new MyClass t2 = new MyClass t3 = new MyClass t4 = new Main() { MyClass(); MyClass(88); MyClass(17.23); MyClass(2, 4); " " " " + + + + t1.x); t2.x); t3.x); t4.x);
Assignment 8
using System; using System.Text; class MainClass { public static void Main() { StringBuilder sb = new StringBuilder("test string"); int length = 0; length = sb.Length; Console.WriteLine("The result is: '{0}'", sb); Console.WriteLine("The length is: {0}", length); } } Assignment9 using System; class MainClass { public static void Main() { int sum = 0; int[] nums = new int[10]; for(int i = 0; i < 10; i++) nums[i] = i; Console.WriteLine("use foreach to display and sum the values"); foreach(int x in nums) { Console.WriteLine("Value is: " + x); sum += x; } Console.WriteLine("Summation: " + sum); } } ASSIGNMENT 10 using System; class MainClass { public static void Main() { // print even numbers between 0 and 100 for(int i = 0; i <= 100; i++) { if((i%2) != 0) continue; // iterate Console.WriteLine(i); } } }