阅读Windows窗体中使用的OpenFileDialog一个文本文件 [英] Reading a text file using OpenFileDialog in windows forms
问题描述
我是新来的的OpenFileDialog功能,但基本想通了。我需要做的就是打开一个文本文件,从文件中读取数据(纯文本),并正确地将数据放置到单独的文本框,在我的应用程序。下面是我在我的打开文件事件处理程序:
I am new to the OpenFileDialog function, but have the basics figured out. What I need to do is open a text file, read the data from the file (text only) and correctly place the data into separate text boxes in my application. Here's what I have in my "open file" event handler:
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)
{
MessageBox.Show(theDialog.FileName.ToString());
}
}
该文本文件我需要阅读这是(对功课,我需要阅读这个确切的文件),它有一个员工号,姓名,地址,工资和工作时间:
The text file I need to read is this (for homework, I need to read this exact file), It has an employee number, name, address, wage, and hours worked:
1
John Merryweather
123 West Main Street
5.00 30
在我得到的文本文件中,有4个员工,在相同的格式后,立即这个信息。你可以看到,员工的工资和工时都在同一行,而不是一个错字。
In the text file I was given, there are 4 more employees with info immediately after this in the same format. You can see that the employee wage and hours are on the same line, not a typo.
我有一个Employee类在这里:
I have an employee class here:
public class Employee
{
//get and set properties for each
public int EmployeeNum { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public double Wage { get; set; }
public double Hours { get; set; }
public void employeeConst() //constructor method
{
EmployeeNum = 0;
Name = "";
Address = "";
Wage = 0.0;
Hours = 0.0;
}
//Method prologue
//calculates employee earnings
//parameters: 2 doubles, hours and wages
//returns: a double, the calculated salary
public static double calcSalary(double h, double w)
{
int OT = 40;
double timeandahalf = 1.5;
double FED = .20;
double STATE = .075;
double OThours = 0;
double OTwage = 0;
double OTpay = 0;
double gross = 0; ;
double net = 0;
double net1 = 0;
double net2 = 0;
if (h > OT)
{
OThours = h - OT;
OTwage = w * timeandahalf;
OTpay = OThours * OTwage;
gross = w * h;
net = gross + OTpay;
}
else
{
net = w * h;
}
net1 = net * FED; //the net after federal taxes
net2 = net * STATE; // the net after state taxes
net = net - (net1 + net2);
return net; //total net
}
}
所以我需要拉文从该文件到我的Employee类,然后将数据输出到正确的文本框的Windows窗体应用程序。我无法理解如何做到这一点的权利。我需要用一个StreamReader?还是有另一种,在这种情况下更好的办法?谢谢
So I need to pull the text from that file into my Employee class, then output the data to the correct textbox in the windows forms application. I am having trouble understanding how to do this right. Do I need to use a streamreader? Or is there another, better way in this instance? Thank you.
推荐答案
下面是一种方法:
Stream myStream = null;
OpenFileDialog theDialog = new OpenFileDialog();
theDialog.Title = "Open Text File";
theDialog.Filter = "TXT files|*.txt";
theDialog.InitialDirectory = @"C:\";
if (theDialog.ShowDialog() == DialogResult.OK)
{
try
{
if ((myStream = theDialog.OpenFile()) != null)
{
using (myStream)
{
// Insert code to read the stream here.
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}
从这里修改:的MSDN OpenFileDialog.OpenFile
修改这里是你的需要另一种方式更适合:
EDIT Here's another way more suited to your needs:
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)
{
string filename = theDialog.FileName;
string[] filelines = File.ReadAllLines(filename);
List<Employee> employeeList = new List<Employee>();
int linesPerEmployee = 4;
int currEmployeeLine = 0;
//parse line by line into instance of employee class
Employee employee = new Employee();
for (int a = 0; a < filelines.Length; a++)
{
//check if to move to next employee
if (a != 0 && a % linesPerEmployee == 0)
{
employeeList.Add(employee);
employee = new Employee();
currEmployeeLine = 1;
}
else
{
currEmployeeLine++;
}
switch (currEmployeeLine)
{
case 1:
employee.EmployeeNum = Convert.ToInt32(filelines[a].Trim());
break;
case 2:
employee.Name = filelines[a].Trim();
break;
case 3:
employee.Address = filelines[a].Trim();
break;
case 4:
string[] splitLines = filelines[a].Split(' ');
employee.Wage = Convert.ToDouble(splitLines[0].Trim());
employee.Hours = Convert.ToDouble(splitLines[1].Trim());
break;
}
}
//Test to see if it works
foreach (Employee emp in employeeList)
{
MessageBox.Show(emp.EmployeeNum + Environment.NewLine +
emp.Name + Environment.NewLine +
emp.Address + Environment.NewLine +
emp.Wage + Environment.NewLine +
emp.Hours + Environment.NewLine);
}
}
}
这篇关于阅读Windows窗体中使用的OpenFileDialog一个文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!