Zde je pro úplnost uvedená kompletní implementace třídy FileUtilities, která byla vytvořená v tomto příspěvku: část 1, část 2
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Collections.Generic;
internal static class FileUtilities
{
#region member types definition
private sealed class WebResponseReader : TextReader, IDisposable
{
#region member varible and default property initialization
private WebResponse response;
private StreamReader reader;
#endregion
#region constructors and destructors
public WebResponseReader(WebRequest request, Encoding encoding)
{
this.response = request.GetResponse();
try
{
this.reader = new StreamReader(response.GetResponseStream(), encoding);
}
catch
{
response.Close();
throw;
}
}
#endregion
#region action methods
public override string ReadLine()
{
return this.reader.ReadLine();
}
#endregion
#region private member functions
protected override void Dispose(bool disposing)
{
if (disposing)
{
reader.Close();
response.Close();
}
}
#endregion
}
#endregion
#region action methods
#if FRAMEWORK35
public static IEnumerable<string> ReadLines(string path, Encoding encoding)
{
if (path == null)
{
throw new ArgumentNullException("path");
}
if (encoding == null)
{
throw new ArgumentNullException("encoding");
}
if (path.Length == 0)
{
throw new ArgumentException("path is empty string.", "path");
}
return ReadLinesInternal(new StreamReader(path, encoding));
}
public static IEnumerable<string> ReadLines(string path)
{
if (path == null)
{
throw new ArgumentNullException("path");
}
if (path.Length == 0)
{
throw new ArgumentException("path is empty string.", "path");
}
return ReadLinesInternal(new StreamReader(path, Encoding.UTF8));
}
#endif
public static IEnumerable<string> ReadLines(Uri url, Encoding encoding)
{
if (url == null)
{
throw new ArgumentNullException("url");
}
if (encoding == null)
{
throw new ArgumentNullException("encoding");
}
return ReadLinesInternal(new WebResponseReader(WebRequest.Create(url), encoding));
}
public static IEnumerable<string> ReadLines(Uri url)
{
if (url == null)
{
throw new ArgumentNullException("url");
}
return ReadLinesInternal(new WebResponseReader(WebRequest.Create(url), Encoding.UTF8));
}
public static IEnumerable<string> ReadHeaderAndLines(string path, Encoding encoding, out string header)
{
if (path == null)
{
throw new ArgumentNullException("path");
}
if (encoding == null)
{
throw new ArgumentNullException("encoding");
}
if (path.Length == 0)
{
throw new ArgumentException("path is empty string.", "path");
}
var reader = new StreamReader(path, encoding);
try
{
header = reader.ReadLine();
if (header == null)
{
throw new EndOfStreamException("Header row not found.");
}
}
catch
{
reader.Dispose();
throw;
}
return ReadLinesInternal(reader);
}
public static IEnumerable<string> ReadHeaderAndLines(string path, out string header)
{
return ReadHeaderAndLines(path, Encoding.UTF8, out header);
}
public static IEnumerable<string> ReadHeaderAndLines(Uri url, Encoding encoding, out string header)
{
if (url == null)
{
throw new ArgumentNullException("url");
}
if (encoding == null)
{
throw new ArgumentNullException("encoding");
}
var reader = new WebResponseReader(WebRequest.Create(url), encoding);
try
{
header = reader.ReadLine();
if (header == null)
{
throw new EndOfStreamException("Header row not found");
}
}
catch
{
reader.Dispose();
throw;
}
return ReadLinesInternal(reader);
}
public static IEnumerable<string> ReadHeaderAndLines(Uri url, out string header)
{
return ReadHeaderAndLines(url, Encoding.UTF8, out header);
}
#endregion
#region private member functions
private static IEnumerable<string> ReadLinesInternal(TextReader reader)
{
using (reader)
{
while (true)
{
string line = reader.ReadLine();
if (line == null)
{
break;
}
yield return line;
}
}
}
#endregion
}