// crudwork
// Copyright 2004 by Steve T. Pham (http://www.crudwork.com)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with This program. If not, see .
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.CodeDom.Compiler;
using System.Text.RegularExpressions;
namespace crudwork.Utilities
{
///
/// File Utility
///
public static class FileUtil
{
#region ReadFile methods
/////
///// Read content of file and store into a string array.
/////
/////
/////
//public static string[] ReadFile(string filename)
//{
// try
// {
// List results = new List();
// using (StreamReader r = new StreamReader(filename))
// {
// while (!r.EndOfStream)
// {
// results.Add(r.ReadLine());
// }
// r.Close();
// }
// return results.ToArray();
// }
// catch (Exception ex)
// {
// DebuggerTool.AddData(ex, "filename", filename);
// throw;
// }
//}
///
/// Read the given filename and yield return a string
///
///
///
public static IEnumerable ReadFile(string filename)
{
using (StreamReader r = new StreamReader(filename))
{
while (!r.EndOfStream)
{
string line = r.ReadLine();
yield return line;
}
r.Close();
}
yield break;
}
/////
///// Read the given filename and return a byte array
/////
/////
/////
/////
//public static byte[] ReadFile(string filename, int bufSize)
//{
// StringBuilder s = new StringBuilder();
// List results = null;
// using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read, bufSize))
// using (BinaryReader r = new BinaryReader(fs))
// {
// results = new List((int)fs.Length);
// byte[] readChar = null;
// do
// {
// readChar = r.ReadBytes(bufSize);
// results.AddRange(readChar);
// } while ((readChar != null) && (readChar.Length > 0));
// r.Close();
// fs.Close();
// }
// return results.ToArray();
//}
///
/// Read the filename and yield return a byte array
///
///
///
///
public static IEnumerable ReadFile(string filename, int bufSize)
{
return ReadFile(filename, bufSize, 0);
}
///
/// Read the filename, start a the specified position, and yield return a byte array
///
/// type input file
/// this bufSize will be multiple by 10
/// set the starting position
///
public static IEnumerable ReadFile(string filename, int bufSize, int startAtPosition)
{
using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read, bufSize * 10))
using (BinaryReader r = new BinaryReader(fs))
{
fs.Position = startAtPosition;
byte[] readChar = null;
do
{
readChar = r.ReadBytes(bufSize);
if (readChar != null)
yield return readChar;
} while ((readChar != null) && (readChar.Length > 0));
r.Close();
fs.Close();
}
yield break;
}
#endregion
}
}