tbf/Workplace/BlackList.cs

87 lines
2.2 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
namespace Workplace
{
public class BlackList
{
/// <summary>
/// List of forbidden black listed items
/// </summary>
public IList<string> Codes;
/// <summary>
/// Default constructor
/// </summary>
public BlackList()
{
Codes = new List<string>();
}
/// <summary>
/// Constructor from a list of black listed codes
/// </summary>
/// <param name="codes"></param>
public BlackList(IList<string> codes)
{
Codes = codes;
}
/// <summary>
/// Read a black listed codes from a file.
/// Each line of text contains one item/code.
/// </summary>
/// <param name="fileName">File name</param>
/// <returns>BlackList object with codes when successful or null</returns>
public static BlackList FromFile(string fileName)
{
try
{
IList<string> codes = new List<string>();
TextReader rdr = new StreamReader(fileName);
string line = rdr.ReadLine();
while (!string.IsNullOrEmpty(line))
{
codes.Add(line);
line = rdr.ReadLine();
}
return new BlackList(codes);
}
catch (Exception)
{
return null;
}
}
/// <summary>
/// Write a black listed codes to a file.
/// Each line of text contains one item/code.
/// </summary>
/// <param name="fileName">File name</param>
/// <returns>true = successful</returns>
public bool ToFile(string fileName)
{
try
{
using (TextWriter wrtr = new StreamWriter(fileName))
{
foreach (var code in Codes)
{
wrtr.WriteLine(code);
}
}
return true;
}
catch (Exception)
{
return false;
}
}
}
}