Search a file that was created
When I create the file and append to it the rest of the information I now want to have the ability to read the text file then display the Month of a birthday thats listed in the file. I want to be able to pull in just the info by birthday. So if I choose month 11 I want to pull in all the data entries that have a birthmonth of 11 by pushing button4.
This is what I have so far;
private void close_Click(object sender, EventArgs e)
{
Close();
}
private void button1_Click(object sender, EventArgs e)
{
writetext();
reset();
}
public void writetext()
{
using (TextWriter writer = File.AppendText("filename.txt"))
{
writer.WriteLine("First name, {0} Lastname, {1} Phone,{2} Day of birth,{3} Month of Birth{4}", textBox1.Text, textBox2.Text, maskedTextBox1.Text, textBox4.Text, textBox3.Text);
MessageBox.Show(String.Format("First Name,{0} Lastname, {1} Phone,{2} Day of birth,{3} Month of Birth{4}", textBox1.Text, textBox2.Text, maskedTextBox1.Text, textBox4.Text, textBox3.Text));
}
}
public void reset()
{
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
maskedTextBox1.Text = "";
}
private void button3_Click(object sender, EventArgs e)
{
Close();
}
private void button2_Click(object sender, EventArgs e)
{
readfile();
}
private void label7_Click(object sender, EventArgs e)
{
}
private void button4_Click(object sender, EventArgs e)
{
}
public void readfile()
{
string[] lines = File.ReadAllLines("filename.txt");
开发者_如何学Go label6.Text = String.Join(Environment.NewLine, lines);
}
}
}
Same as Geoffrey but with C# and linq syntax:
You can filter the content of your file using the birth date and store it in a new array this way:
string[] persons = File.ReadAllLines(Server.MapPath("filename.txt"));
IEnumerable<string> personsWithBirthday =
from p in persons
where p.Contains("1980-08-21")
select p;
foreach (var person in personsWithBirthday)
Response.Write(person);
I would recommend you to define a standard date format in order to easily grab all the persons matching the search criteria.
Please forgive me for using vb.net syntax here as I am more acustomed to it. The translation should be quite straightforward though:
Dim rdr As New IO.StreamReader("filename.txt")
While rdr.Peek <> -1
Dim strLine as String = rdr.ReadLine()
If strLine.Contains("Birth Month,11") Then
Label6.Text &= strLine
End If
End While
rdr.Close()
Hope that helps.
精彩评论