我有两个方法,一个是读文件的(LoadList),另一个是写文件的(SaveList),但是一执行了LoadList读文件的,SaveList后面保存就失败了。哪位大哥帮忙看看哪里有错
private void SaveList(string path)
{
string proxyText = "";
foreach (ListViewItem lvi in listView1.Items)
{
proxyText += lvi.SubItems[0].Text + ":";
proxyText += lvi.SubItems[1].Text + "\r\n";
}
FileStream fs = new FileStream(path,FileMode.Create);
byte[] b = Encoding.UTF8.GetBytes(proxyText);
fs.Write(b,0,b.Length);
fs.Flush();
fs.Close();
//MessageBox.Show("已经自动保存" + Encoding.UTF8.GetBytes(proxyText));
}
private void LoadList(string path)
{
if (!File.Exists(path))
{
File.Create(path);
}
string proxyText = "";
Regex reg = new Regex(regex);
using (FileStream fs = File.OpenRead(path))
{
byte[] b = new byte[fs.Length];
fs.Read(b, 0, b.Length);
fs.Flush();
fs.Close();
proxyText = Encoding.UTF8.GetString(b);
MatchCollection matches = reg.Matches(proxyText);
for (int i = 0; i < matches.Count; i++)
{
string proxyIpAndPort = matches[i].Groups[0].Value;
ListViewItem l = new ListViewItem();
l.SubItems[0].Text = proxyIpAndPort.Substring(0, proxyIpAndPort.IndexOf(':'));
l.SubItems.Add(proxyIpAndPort.Substring(proxyIpAndPort.IndexOf(':') + 1, proxyIpAndPort.Length - proxyIpAndPort.IndexOf(':') - 1));
listView1.Items.Add(l);
}
}
}