Skip to content

Instantly share code, notes, and snippets.

@zharro
Last active July 29, 2016 11:49
Show Gist options
  • Save zharro/cd0a65182c50b531856a17d30d2da333 to your computer and use it in GitHub Desktop.
Save zharro/cd0a65182c50b531856a17d30d2da333 to your computer and use it in GitHub Desktop.
Interacting with file system
/// <summary>
/// Agent for interacting with file system
/// </summary>
public class FileSystemAgent : IFileSystemAgent
{
public IList<string> ReadAllLines(string fileName)
{
if (!File.Exists(fileName))
{
throw new ArgumentException($"File {fileName} doesn't exist");
}
return File.ReadAllLines(fileName).ToList();
}
public IEnumerable<string> ReadLineByLine(string fileName)
{
if (!File.Exists(fileName))
{
throw new ArgumentException($"File {fileName} doesn't exist");
}
using (var reader = File.OpenText(fileName))
{
string line;
while ((line = reader.ReadLine()) != null)
{
yield return line;
}
}
}
public void WriteAllLines(IEnumerable<string> lines, string fileName)
{
using (StreamWriter file = new StreamWriter(fileName))
{
foreach (string line in lines)
{
file.WriteLine(line);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment