【发布时间】:2013-08-02 21:05:06
【问题描述】:
我是 C# 中使用文件处理程序的新手。我已经完成了插入和搜索。请帮助我如何使用以下代码进行更新和删除。
界面部分::
private void btnAdd1_Click(object sender, EventArgs e)
{
StudentManager sm = new StudentManager();
sm.AddStudent(textName.Text,textId.Text,textDpt.Text,textSem.Text);
}
public void btnSearch1_Click(object sender, EventArgs e)
{
StudentManager sm = new StudentManager();
Student s = sm.FindStudent(textId.Text);
if (s != null)
{
this.textName.Text = s.GetName();
this.textId.Text = s.ID;
this.textDpt.Text = s.Department;
this.textSem.Text = s.GetSEM();
}
}
验证::
string id = String.Empty;
public void SetName(string name)
{
if(!String.IsNullOrEmpty(name))
{
this.name = name;
}
}
public string ID
{
get { return id; }
set { id = value; }
}
string department = String.Empty;
public string Department
{
get { return department; }
set { department = value; }
}
string SEM= String.Empty;
public void SetSEM(string sem)
{
if (!String.IsNullOrEmpty(sem))
{
this.SEM = sem;
}
}
public string GetSEM()
{
return this.SEM;
}
public String GetName()
{
return this.name;
}
}
学生经理::
class StudentManager
{
ArrayList students;
const string FILENAME = @"d:\students.txt";
public StudentManager()
{
SetStudentList();
}
private void SetStudentList()
{
if (students == null)
{
//create a file handler
FileHandler sfh = new FileHandler();
//initialize the teacher list object
students = new ArrayList();
//Now read all the lines from the teacher.txt
//each line represent a teacher
string[] studentfromfile = sfh.getAllLines(@FILENAME);
int totalstudents = studentfromfile.Length;
//go through each teacher and create teacher object to add it to the teacher list.
for (int i = 0; i < totalstudents; i++)
{
string studentinfo = studentfromfile[i];
string[] studentinfobroken = studentinfo.Split(new char[] { ',' });
if (studentinfobroken.Length == 4)
{
//this part is being duplicated - can think how?
Student s = new Student();
s.SetName(studentinfobroken[0]);
s.ID= studentinfobroken[1];
s.Department = studentinfobroken[2];
s.SetSEM(studentinfobroken[3]);
this.students.Add(s);
}
}
}
}
public void AddStudent(string fullname, string ID, string dept,string Semester )
{
Student s = new Student();
s.SetName(fullname);
s.ID = ID;
s.Department = dept;
s.SetSEM(Semester);
this.students.Add(s);
FileHandler sfh = new FileHandler();
string studentInfo = Environment.NewLine + s.GetName() + "," + s.ID + "," + s.Department + "," + s.GetSEM();
sfh.AddLine(@FILENAME, studentInfo);
}
public Student FindStudent(string ID)
{
foreach (Student s in students)
{
if (s.ID.Equals(ID))
{
return s;
}
}
return null;
}
class FileHandler
{
public String[] getAllLines(string fileName)
{
try
{
String[] lines = File.ReadAllLines(@fileName);
return lines;
}
catch (Exception e)
{
throw e;
}
}
public String GetAllText(string fileName)
{
try
{
String content = File.ReadAllText(@fileName);
return content;
}
catch (Exception e)
{
throw e;
}
}
public void AddLine(string filename, string line)
{
StreamWriter sr = null;
try
{
sr = new StreamWriter(@filename, true);//true for append
sr.WriteLine(line);
}
catch (Exception e)
{
throw e;
}
finally
{
sr.Close();
}
}
【问题讨论】:
-
您要更新和删除哪一行?你是怎么找到的?
-
老实说,我刚读完你的开场白就直接滚动到评论部分。您应该只包含相关的代码,这会让人们望而却步
-
首先我在我的 student.txt 文件中插入了一些行..然后我通过 id 搜索特定的学生信息..然后我想删除该学生信息..并更新!!请先生帮助我..
-
不要发布您的所有代码。这不是代码审查会议,发布仅与您的问题相关的代码。 Google 和 Stackoverflow 上也有大量示例,如果您进行简单搜索,您现在可能已经有了解决方案
-
文本文件是如何格式化的? ID 出现在行中的什么位置?您提供了足够的代码(太多了!:)),但没有足够的相关细节信息。
标签: c# winforms text-files filehandler