using System; using System.IO; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; namespace EdgeTech.SonarComms.Demo { class CReadIniFile { const string _versionInfo = "$Header: CReadIniFile.cs Revision:1.7 Thu Apr 19 12:36:20 2012 mps $"; private string versionInfo; public static byte[] ReadFile(string filePath) { byte[] buffer; FileStream fileStream = null; if (Utils.doThisPrint(4)) Console.WriteLine("Using initialization file {0}", filePath); if (Utils.doThisPrint(5)) Console.WriteLine("---------------------------------------- "); try { fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read); int length = (int)fileStream.Length; // get file length buffer = new byte[length]; // create buffer int count; // actual number of bytes read int sum = 0; // total number of bytes read // read until Read method returns 0 (end of the stream has been reached) while ((count = fileStream.Read(buffer, sum, length - sum)) > 0) { sum += count; // sum is a buffer offset for next reading } } catch (FileNotFoundException) { if(Utils.doThisPrint(1)) Console.WriteLine("Error: Initialization file {0} not found. Now exiting.", filePath ); return null; } finally { if(fileStream != null) fileStream.Close(); } return buffer; } public string getVersionInfo() { versionInfo = _versionInfo.Remove(0, 9); string endStr = " mps $"; char[] end = endStr.ToCharArray(); versionInfo = versionInfo.TrimEnd(end); return versionInfo; } } }