我不确定为什么我的文件没有在我的C#程序中读取。当我运行它时,它只在文本框中给出一个0,并且不列出任何名称。我想知道它是否与其他东西有问题,它正在正确读取文件。感谢大家的帮助!
以下是调用该文件的CustomerDA类,我不确定它是否正确:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace CustomerList
{
public static class CustomerDA
{
private const string dir = @"Z:\Desktop\Windows 7 Files\C#.net\CustomerList\CustomerList";
private const string path = dir + "CustomerDat.txt";
public static List<Customer> GetAllCustomers()
{
if (!Directory.Exists(dir))
Directory.CreateDirectory(dir);
StreamReader textIn =
new StreamReader(
new FileStream(path, FileMode.Open, FileAccess.Read));
List<Customer> custList = new List<Customer>(); // this create the list to add the Customers to.
while (textIn.Peek() != -1)
{
string row = textIn.ReadLine();
string[] columns = row.Split(',');
Customer customer = new Customer();
customer.Name = columns[0];
customer.Email = columns[1];
customer.CreditRating = Convert.ToInt16(columns[2]);
customer.City = columns[3];
customer.Zip = columns[4];
}
textIn.Close();
// use the method to read found in the book as an example on page 677
// The file to read can be found on Moodle CustomerDat.txt
// I also placed a copy in this project.
//once all of the Customers have been read into the List return the list
return custList;
}
}
}
这是Form1.cs,我不确定我是否正确调用它。对我来说一切似乎都是正确的:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CustomerList
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
List<Customer> ourCustomers = new List<Customer>();
private void Form1_Load(object sender, EventArgs e)
{
// call the CustomerDa Class and read all of the Customers from the file and return the list back here
ourCustomers = CustomerDA.GetAllCustomers();
}
private void btnShowGood_Click(object sender, EventArgs e)
{
int numGoodCustomers = 0;
// loop through all of the customers returned from the file
foreach (Customer cust in ourCustomers)
{
// see if the customer has a rating > 8
if(cust.CreditRating > 8)
{
// add the name to the listbox
lstGoodCustomers.Items.Add(cust.Name);
// add one to the count of good customers
numGoodCustomers ++;
}
}
// Update the form with the number of good Customers
txtNumGoodCustomers.Text = numGoodCustomers.ToString();
}
}
}
答案 0 :(得分:4)
您在阅读时从不添加到列表中,因此它始终为空。将custList.Add(customer)
添加到循环的末尾。
private const string dir = @"Z:\Desktop\Windows 7 Files\C#.net\CustomerList\CustomerList";
private const string path = Path.Combine(dir, "CustomerDat.txt");
List<Customer> custList = new List<Customer>(); // this create the list to add the Customers to.
while (textIn.Peek() != -1)
{
string row = textIn.ReadLine();
string[] columns = row.Split(',');
Customer customer = new Customer();
customer.Name = columns[0];
customer.Email = columns[1];
customer.CreditRating = Convert.ToInt16(columns[2]);
customer.City = columns[3];
customer.Zip = columns[4];
custList.Add(customer); //This was missing
}
正如@Gusman所说(添加到我的完整性答案中),您需要使用Path.Combine
代替&#39; +&#39;同样,你的路径也是无效的。
答案 1 :(得分:2)
好的,很简单,使用ALWAYS Path.Combine连接两个路径元素。
我为什么这么说?
看,你有dir = @&#34; Z:\ Desktop \ Windows 7 Files \ C#.net \ CustomerList \ CustomerList&#34;和path = dir +&#34; CustomerDat.txt&#34;,所以路径是@&#34; Z:\ Desktop \ Windows 7 Files \ C#.net \ CustomerList \ CustomerListCustomerDat.txt&#34;。
你错过了&#34; \&#34;,使用Path.Combine可以避免这些问题。