所以我想要做的就是读取一个文件,该文件具有一个像这样读取的数据段。到目前为止,程序从下拉菜单中打开文件,但我很难将它们保存到数组中。我希望能够在窗体应用程序中打开文件(它将文本文件的最后三行打印到文本框中)后单击下一个按钮,并将下面的文本文件示例中的每条信息行打印到单独的文本框。这是我遇到问题的地方。从文本文件中读取多行并将其保存到数组中的问题c#
姓名和地址将被保存到一个EmpNames类中,然后将下面的.split()
数字保存到它们各自的Employee Class中,以便设置为一系列计算,然后将结果打印到文本框。
1
John MerryWeather
123 West Main Street
5.00 30
会有像这样的多个数据段,但不会超过10个。这是迄今为止我所拥有的。
public partial class Form1 : Form
{
const int MAX = 10;
public Form1()
{
InitializeComponent();
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog theDialog = new OpenFileDialog();
theDialog.Title = "Open Text File";
theDialog.Filter = "TXT files|*.txt";
theDialog.InitialDirectory = @"C:\";
if (theDialog.ShowDialog() == DialogResult.OK)
{
//Declarations:
// linesPerEmployee: Controls the number of lines to be read.
// currEmployeeLine: Controls where in the file you are reading.
Employee employee = new Employee();
NameAdd empNames = new NameAdd();
string filename = theDialog.FileName;
List<Employee> employeeList = new List<Employee>();
int linesPerEmployee = 4;
int currEmployeeLine = 0;
//parse line by line into instance of employee class
while (employeeList != null)
{
string[] filelines = File.ReadAllLines(filename);
if (filelines != null)
{
employee.EmpNum = int.Parse(filelines[0]);
empNames.Name =
}
}
指定与例如 –
刚刚与一个简单的.txt文件的文件格式格式如上所示。将会有多个这样的四个线段,每个线段从1开始递增,然后是名称,地址和小时工资,然后是2,3等等。 –
您可以使用skip跳过最后4行,并采用linq的扩展。然后把它们放在一个数组中 – Saravanan