Здравствуйте, kudzu, Вы писали:
Можно вот так:
[Serializable()]
public struct FileRecord
{
public int FN { get; set; }
public int SN { get; set; }
public int BN { get; set; }
public const string ADD = "ADD BRD:";
public const string BP = "BACK";
public const string BT = "E32";
public const string HBT = "ME32";
public const string BS = "LOADSHARE";
public const string ADS = "ACTIVE";
public FileRecord(int fn, int sn, int bn) : this()
{
FN = fn;
SN = sn;
BN = bn;
}
}
public class Data
{
public string FileName { get; set; }
public Data(string fileName)
{
this.FileName = fileName;
}
private Dictionary<int, FileRecord> AddValues(int startDictionaryKey)
{
return this.SelectCorrectionInformation().ToDictionary(key => startDictionaryKey++);
}
public IEnumerable<string> PrintValues()
{
var dic = this.AddValues(0);
foreach (KeyValuePair<int, FileRecord> pair in dic)
{
yield return String.Format("FN = {0}, SN = {1}, BN = {2}",
pair.Value.FN,
pair.Value.SN,
pair.Value.BN);
}
}
private IEnumerable<FileRecord> SelectCorrectionInformation()
{
var buf = File.ReadAllLines(this.FileName);
foreach (var i in buf)
{
var data = new FileRecord(
GetNoFixedValue(i, 0),
GetNoFixedValue(i, 1),
GetNoFixedValue(i, 6));
yield return data;
}
}
private int GetNoFixedValue(string input, int index)
{
return Convert.ToInt32(this.GetNumbersFrom(input.Split(',')[index]));
}
private string GetNumbersFrom(string input)
{
return new String(input.Where(value => "0123456789".Contains(value)).ToArray());
}
Использование:
static void Main(string[] args)
{
Data data = new Data("TextFile1.txt");
var values = data.PrintValues();
foreach (var i in values)
{
Console.WriteLine(i);
}
Console.ReadKey();
}
Результат:
FN = 1, SN = 2, BN = 9
FN = 1, SN = 3, BN = 16
FN = 1, SN = 15, BN = 3
Вот полезная ссылка:
Про метод ToDictionary