#region ListView列表导出与导入
/// <summary>
/// ListView列表保存
/// </summary>
/// <param name="listViewname">ListView名称</param>
/// <param name="path">文件名称</param>
public static void ListView_save(ListView listViewname, string path)
{
if (listViewname.Items.Count != 0)
{
List<string> list = new List<string>();
foreach (ListViewItem item in listViewname.Items)
{
string temp2 = "";
for (int i = 0; i < item.SubItems.Count; i++)
{
if (i == 0)
{
temp2 = item.SubItems[i].Text;
}
else
{
temp2 = temp2 + "|" + item.SubItems[i].Text;
}
}
string temp = temp2;
list.Add(temp);
}
Thread thexp = new Thread(() => export(list, path)) { IsBackground = true };
thexp.Start();
}
}
public static void export(List<string> list, string path)
{
string path2 = path;
if (path.Substring(0, 1) == @"\")
{
path2 = AppDomain.CurrentDomain.BaseDirectory + path; ;
}
StringBuilder sb = new StringBuilder();
foreach (string tel in list)
{
sb.AppendLine(tel);
}
System.IO.File.WriteAllText(path2, sb.ToString(), Encoding.UTF8);
}
/// <summary>
/// ListView列表读入
/// </summary>
/// <param name="listViewname">ListView名称</param>
/// <param name="path">文件名称</param>
public static void ListView_read(ListView listViewname, string path)
{
string path2 = path;
if (path.Substring(0, 1) == @"\")
{
path2 = AppDomain.CurrentDomain.BaseDirectory + path; ;
}
//判断文件的存在
if (File.Exists(path2))
{
//存在文件
foreach (var item in File.ReadAllLines(path2))
{
string[] str = item.Split('|');
if (str.Length > 0)
{
ListViewItem lvi = new ListViewItem();
for (int i = 0; i < str.Length; i++)
{
if (i == 0)
{
lvi.Text = str[i];
}
else
{
lvi.SubItems.Add(str[i]);
}
}
listViewname.Items.Add(lvi);
}
//MessageBox.Show(item);
}
}
}
#endregion