To może wyglądać mniej więcej tak:
using System;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Drawing;
using System.IO;
class Lines
{
public static void SaveRandom(string path, int numberOfLines, int lineLength)
{
using (var file = new FileStream(path, FileMode.Create))
{
using(var stream = new StreamWriter(file))
{
var rnd = new Random();
for (int i = 0; i < numberOfLines; i++)
{
var line = "";
for (int j = 0; j < lineLength; j++)
{
line = line + (char)rnd.Next(97, 123);
}
stream.WriteLine(line);
}
}
}
}
public static void SaveSpecified(string outPath, int numberOfLinesToSave, List<Row> container)
{
using (var file = new FileStream(outPath, FileMode.Create))
{
using (var stream = new StreamWriter(file))
{
for(int i = 0; i < numberOfLinesToSave; i++)
{
if (container[i].Count <= 100)
{
stream.WriteLine(container[i].Text);
container[i].Count += 1;
}
}
}
}
}
public static List<Row> LoadData(string path)
{
var result = new List<Row>();
var lines = File.ReadAllLines(path);
for (int i = 0; i < lines.Length; i++)
{
result.Add(new Row
{
Text = lines[i],
Count = 0
});
}
return result;
}
}
class Row
{
public string Text { get; set; }
public int Count { get; set; }
}
class Program {
public static void Main(string[] args) {
string path = "test.txt";
Lines.SaveRandom(path, 1000, 30);
var form = new Form();
var grid = new DataGridView();
grid.Dock = DockStyle.Fill;
grid.DataSource = Lines.LoadData(path);
var textbox = new TextBox();
textbox.Dock = DockStyle.Fill;
var button = new Button();
button.Text = "Save";
button.Dock = DockStyle.Fill;
button.Click += (sender, e) =>
{
var output = "output.txt";
var count = 0;
if (Int32.TryParse(textbox.Text, out count))
{
Lines.SaveSpecified(output, count, grid.DataSource as List<Row>);
grid.Refresh();
}
};
var panel = new TableLayoutPanel();
panel.Dock = DockStyle.Fill;
panel.Controls.Add(textbox);
panel.Controls.Add(button);
panel.Controls.Add(grid);
form.Controls.Add(panel);
form.ShowDialog();
}
}
Wybaczcie jakość kodu ale już trochę zmęczony jestem. Timera z odliczaniem godzin napisz sobie już sam :)