操纵txt文本文件
txt文本文件(ticket.txt)的格式:
666666,Alfred Lv,Resigned,06/24/2010 02:13:30 PM ZE8
99999,Alfred Lv,Closed,06/24/2010 02:13:45 PM ZE8
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.IO; using System.Text; namespace WebApplication1 { class FileUtilitity { public Ticket[] ReadFile() { string filename = "D:\\Ticket.txt"; string[] Tickets = new string[4]; List<Ticket> ticket = new List<Ticket>(); //打开文件并显示其内容 StreamReader reader = null; try { reader = new StreamReader(filename); for (string line = reader.ReadLine(); !String.IsNullOrEmpty(line); line = reader.ReadLine()) { Tickets = line.Split(','); Ticket tmpTicket= new Ticket(); tmpTicket.TicketNo = Tickets[0]; tmpTicket.Author = Tickets[1]; tmpTicket.Status = Tickets[2]; tmpTicket.LastModified = Tickets[3]; ticket.Add(tmpTicket); } } catch (Exception e) { Console.WriteLine(e.Message); } finally { if (reader != null) reader.Close(); } return ticket.ToArray(); } public void WriteFile(string s, string path) { FileStream fout = new FileStream(path, FileMode.Append, FileAccess.Write); StreamWriter brout = new StreamWriter(fout, Encoding.Default); brout.WriteLine(s); brout.Close(); } } public class Ticket { public string TicketNo { get; set; } public string Status { get; set; } public string Author { get; set; } public string LastModified { get; set; } } }