程式碼
using System; using System.IO; using System.Collections; namespace ConsoleApplication1 { public class CheckSum { private long sum = 0; public CheckSum() { } public void Add(int i) { sum += i; } public void Add(byte b) { sum += b; } public long GetCheckSum() { return sum; } } public class MyClass { public static void Main() { string filename; FileStream fs = null; CheckSum sum = new CheckSum(); //Open the stream and read it back. Console.Write("Please input filename:"); filename = Console.ReadLine(); Console.WriteLine("Trying to read {0}", filename); if (File.Exists(filename)) Console.WriteLine("{0} is existed.", filename); try { fs = File.Open(filename, FileMode.Open, FileAccess.Read); if (fs.CanRead) { int value = 0; while ((value = fs.ReadByte()) >= 0) { sum.Add(value); } Console.WriteLine("Checksum = {0,8:x}", sum.GetCheckSum()); } else Console.WriteLine("Cannot be read"); } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { fs.Close(); } RL(); } #region Helper methods private static void RL() { Console.ReadLine(); } #endregion } }
沒有留言:
張貼留言