public Hashtable txt2hash(FileInfo file)
{
StreamReader sr = new StreamReader(file.FullName, Encoding.UTF8);
Hashtable ht = new Hashtable();
int k = 0;
string nextline = sr.ReadLine();
while (nextline != null)
{
k = nextline.IndexOf("&", 0);//&是键和值的分隔符
string key1 = nextline.Substring(0, k).ToString();
string value1 = nextline.Substring(k + 1, nextline.Length - k - 1).ToString();
if (!ht.Contains(key1))
{
ht.Add(key1, value1);
}
nextline = sr.ReadLine();
}
sr.Close();
return ht;
//IDictionaryEnumerator de = ht.GetEnumerator();//测试输出
// while (de.MoveNext())
// {
// Response.Write(de.Key.ToString()+de.Value.ToString()+"<br>");
// }
}
public void hash2txt(Hashtable ht)
{
Random r = new Random();//随机产生文件名保存成txt文件
string name = r.Next(1000).ToString();
FileInfo myfile = new FileInfo(Server.MapPath("~/index/" + name + ".txt"));
StreamWriter sw = myfile.CreateText();
IDictionaryEnumerator de = ht.GetEnumerator();
while (de.MoveNext())
{
sw.WriteLine(de.Key.ToString() + "&" + de.Value.ToString());
}
sw.Close();
}