• listview1 保存和读取 listViewItems保存为txt


       /*
             *   保存原理
             *   将LISTVIEW视为一行一行的字符串
             *   将所有的行合并成一个字符串 然后保存为TXT文件
             *  
    * 而每行又是由 几个 子项组成,先把子项连接成字符串
             *   2019年11月27日 18:03:17
             */

    源文件:https://download.csdn.net/download/u012663700/12001338

     1 using System;
     2 using System.Collections;
     3 using System.Collections.Generic;
     4 using System.ComponentModel;
     5 using System.Data;
     6 using System.Drawing;
     7 using System.IO;
     8 using System.Linq;
     9 using System.Text;
    10 using System.Windows.Forms;
    11 
    12 namespace ListViewItemsReadWrite
    13 {
    14     public partial class Form1 : Form
    15     {
    16         public Form1()
    17         {
    18             InitializeComponent();
    19         }
    20 
    21         //保存到文件,没有指定路径则路径就是当前EXE的路径
    22         string conf = "test.txt";
    23         /*
    24          *   保存原理
    25          *   将LISTVIEW视为一行一行的字符串
    26          *   将所有的行合并成一个字符串
    27          *   
    28          *   而每行又是由 几个 子项组成,先把子项连接成字符串
    29          *   2019年11月27日 18:03:17
    30          */
    31 
    32          //SAVE
    33         private void button1_Click(object sender, EventArgs e)
    34         {
    35             //将listView中的内容保存成TXT格式
    36             string[] arr = new string[listView1.Items.Count];
    37             for (int i = 0; i < listView1.Items.Count; i++)
    38             {
    39                 string s = "";
    40                 int len = listView1.Items[0].SubItems.Count;
    41                 for (int j = 0; j < len; j++)
    42                 {
    43                     //用逗号作为分割符 如果 有子项中包含逗号 要出问题的
    44                     s += SqlString.Transform(listView1.Items[i].SubItems[j].Text) + ",";
    45                 }
    46 
    47                 if (s.EndsWith(","))
    48                     s = s.Substring(0, s.Length - 1);
    49                 
    50                 arr[i] = s;
    51             }
    52             //保存到文件
    53             File.WriteAllLines(conf, arr);
    54         }
    55 
    56         //READ
    57         private void button2_Click(object sender, EventArgs e)
    58         {
    59             //从文本中读取数据到listView当中
    60 
    61             string[] array = File.ReadAllLines(conf);
    62 
    63             listView1.Items.Clear();
    64             for (int i = 0; i < array.Length; i++)
    65             {
    66                 if (array[i].Length > 0)//不添加空行
    67                 {
    68                     ListViewItem listviewItem = new ListViewItem();
    69                     string s = array[i].ToString();
    70 
    71                     //这里没有对格式是否符合要求进行判断,如果内容被修改成其他格式则会出异常的
    72                     string[] arr = s.Split(',');  
    73                     listviewItem.Text = SqlString.Restore(arr[0]);
    74                     listviewItem.SubItems.Add(SqlString.Restore(arr[1]));
    75                     listviewItem.SubItems.Add(SqlString.Restore(arr[2]));
    76                     listviewItem.SubItems.Add(SqlString.Restore(arr[3]));
    77                     listView1.Items.Add(listviewItem);
    78                 }
    79             }
    80         }
    81 
    82         private void button3_Click(object sender, EventArgs e)
    83         {
    84             listView1.Items.Clear();
    85         }
    86     }
    87 }
  • 相关阅读:
    如何在ASP.NET Core中使用JSON Patch
    IDEA中Maven依赖更新问题 国
    vue实现文字上下滚动效果
    mac 安装 homebrew 超快镜像
    linux查找文件命令有哪些
    Git 操作的 token 验证
    mysql重置主键
    PHP获取特定字符串中间的内容
    MySQL 连接出现 Authentication plugin ‘caching_sha2_password‘ cannot be loaded
    oracle mysql sqlserver三种数据库的查看索引和新增普通索引sql语句
  • 原文地址:https://www.cnblogs.com/xe2011/p/11944416.html
Copyright © 2020-2023  润新知