streamwriter sw= new streamwriter(filepath, true);//最后这个true表示追加,false表示覆盖
sw.writeline(content );
sw.close();///纯手打,大小写自己改至于你要的重复不重复问题,这个比较严重了,毕竟我不太清楚你的设计,如果数据量多的话,你还是建个数据库,写到数据库里比较快,不然数据一多这效率实在低下,给你找了点代码,自己参考一下。var file = file.open("d:\\xxx.txt", filemode.open);
list<string> txt = new list<string>();
using (var stream = new streamreadr(file))
{
while (!stream.endofstream)
{
txt.add(stream.readline());
}
}
file.Close();然后你自己写上自己后面的需求吧
sw.writeline(content );
sw.close();///纯手打,大小写自己改至于你要的重复不重复问题,这个比较严重了,毕竟我不太清楚你的设计,如果数据量多的话,你还是建个数据库,写到数据库里比较快,不然数据一多这效率实在低下,给你找了点代码,自己参考一下。var file = file.open("d:\\xxx.txt", filemode.open);
list<string> txt = new list<string>();
using (var stream = new streamreadr(file))
{
while (!stream.endofstream)
{
txt.add(stream.readline());
}
}
file.Close();然后你自己写上自己后面的需求吧
WeiteFile
CloseFile
详情参考MSDN
{ string[] ips = {"127.0.0.1",
"127.0.0.2",
"127.0.0.1"};
foreach (string ip in ips)
{
StreamReader sr = new StreamReader("..\\..\\file\\IP.txt");
string filecontext = sr.ReadToEnd();
sr.Close();
filecontext = filecontext.Replace("\r\n","#");
string[] filerows = filecontext.Split('#'); if( string.IsNullOrEmpty(filerows.FirstOrDefault(filerow => filerow.Equals(ip))))
{
StreamWriter sw = new StreamWriter("..\\..\\file\\IP.txt", true);
sw.WriteLine(ip);
sw.Close();
}
}
}
另外再请教个问题,换上输入实现后,能否实现,让后添加的数据在上面一行显示,之前添加的数据自动变成第二行,以此类推。先把整个文件读成一个STRING,然后再输出成一个文件,内容是:
newFileContent + "\\r\\n" + OldFileContent
不过这里注意一点换行