我们在做用户注册时候,经常要预留出用户名保留,不让用户时候,那么我们的保留用户名放在什么地方呢,可以有以下选择,1.数据库 2.config文件中 3 文本文件中,那么我们怎么选择呢,我的建议是选择后两条,那么一般的为什么我们不放在数据库中呢,因为这是网站配置问题,放在数据库中只有一条记录,一个表中方一跳纪录,浪费资源,虽然说数据库容易操作,好像大材小用,有的人习惯放在.config文件中,我有的时候也放在.config中,但是有时候对于变化不大的东西就放在.txt文件中,下面就说一下,如果放在.txt文件中怎么读呢?首先引用以下命名空间
System.IO
之后读入流放在数组中
protected string[] GetStopWords()
{
using (StreamReader reader = new StreamReader(Server.MapPath("~" + "\\App_Data\\stopwords.txt")))
{
string words = reader.ReadToEnd();
return words.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
}
}
有了这个数组,一些就好办了,循环输出
{
using (StreamReader reader = new StreamReader(Server.MapPath("~" + "\\App_Data\\stopwords.txt")))
{
string words = reader.ReadToEnd();
return words.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
}
}
string[] s = GetStopWords();
for (int i = 0; i < s.Length;i++ )
{
Response.Write(s[i]+"<br/>");
}
for (int i = 0; i < s.Length;i++ )
{
Response.Write(s[i]+"<br/>");
}