当前位置:网站首页>C#读写txt文件到listview
C#读写txt文件到listview
2022-06-22 08:01:00 【shixch】
C#读写txt文件到listview
读txt文件
private void button2_Click_2(object sender, EventArgs e)
{
string[] array = File.ReadAllLines(conf);
listView1.Items.Clear();
for (int i = 0; i < array.Length; i++)
{
if (array[i].Length > 0)//不添加空行
{
ListViewItem listviewItem = new ListViewItem();
string s = array[i].ToString();
//这里没有对格式是否符合要求进行判断,如果内容被修改成其他格式则会出异常的
string[] arr = s.Split(',');
listviewItem.Text = (i + 1).ToString();
listviewItem.SubItems.Add(arr[0]);
listviewItem.SubItems.Add(arr[1]);
listviewItem.SubItems.Add(arr[2]);
listView1.Items.Add(listviewItem);
}
}
}
写txt
private void button3_Click_1(object sender, EventArgs e)
{
//将listView中的内容保存成TXT格式
string[] arr = new string[listView1.Items.Count];
for (int i = 0; i < listView1.Items.Count; i++)
{
string s = "";
int len = listView1.Items[0].SubItems.Count;
for (int j = 1; j < len; j++)
{
//用逗号作为分割符 如果 有子项中包含逗号 要出问题的
s += listView1.Items[i].SubItems[j].Text + ",";
}
if (s.EndsWith(","))
s = s.Substring(0, s.Length - 1);
arr[i] = s;
}
//保存到文件
File.WriteAllLines(conf, arr);
}
边栏推荐
- (9) Sequential queue and stack queue
- A glimpse of easy rule
- Realization of readable and writable files in library management system with C language bidirectional linked list
- Spritemanager load Atlas
- MySQL intercepts the string cs0000_ 1 medium_ Following characters
- DTD约束
- JS to assign values to two objects with the same attributes
- Mystery of power bank
- Common array operations in JS
- Vue page caching problem solving (keep alive + page level routing guard + lifecycle activated)
猜你喜欢

【Oracle 数据库】奶妈式教程 day12 字符函数

Detailed explanation of subnet mask

Some suggestions on Oracle SQL query return optimization

充电宝的玄机

Note pad replaces all contents after a character in all lines

Xlua environment configuration

Expérience électrique en mode - - expérience 2 circuit d'amplification de source commune JFET

Applet /vant UI to upload files

Template code overview

How Navicat queries the password information of the connected database
随机推荐
关于菲涅尔现象
C# 接口持有结构体会导致装箱问题
【宋红康 MySQL数据库 】【高级篇】【07】MySQL的存储引擎
Node red sends wechat official account message (template message)
Problems caused by canvas palette width and height and canvas width and height
DTD约束
Spritemanager load Atlas
CollectionViewCell
Oracle gets the working day time between two dates
Mt4/mql4 getting started to mastering EA tutorial lesson 3 - common functions of MQL language (III) - common functions of K-line value taking
【 Oracle database】 Nursery Mother Tutorial day13 date Function
Kubernetes practice
Note pad replaces all contents after a character in all lines
Modular import and export collation in JS
Windchill API drops
《守望先锋》阵亡镜头、全场最佳和亮眼表现是如何设计
QT QtableView的使用示例
【Oracle 数据库】奶妈式教程day15 DDL、DML、索引、视图、序列、死锁这一篇就够了
[Oracle database] mammy tutorial day14 conversion function
Master data management system