大家好,尝试在某些 csv 文件中实现删除行;文件示例:
24 august 2013 г.,,14:00,00:00,;
24 august 2013 г.,,14:00,00:00,;
24 august 2013 г.,2342,14:00,00:00,23424;
24 august 2013 г.,2342,14:00,19:00,23424;
criteria - 24 august 2013 г.,2342
结果必须像
24 august 2013 г.,,14:00,00:00,;
24 august 2013 г.,,14:00,00:00,;
我的想法打开文件-
FileStream fs = new FileStream(filePath, FileMode.Open,
FileAccess.ReadWrite);
StreamReader sr = new StreamReader(fs);
将所有数据放入数组
string lines = sr.ReadToEnd();
string []result = lines.Split(';');
比获取删除数据的标准(一些字符串)
string criteria = dateTimePicker1.Text.ToString()+','
+eventNameDeleteTextBox.Text.ToString();
sr.Close();
在数组中搜索它,
int startStr = lines.IndexOf(criteria);//find start position
int length = lines.IndexOf(';',startStr)-startStr;//find end position
删除字符串 dataRemoved = lines.Substring(startStr,length); viewTextBox.Text = dataRemoved;
并写入文件更新数据
FileStream fs1 = new FileStream(filePath, FileMode.Open,
FileAccess.Write, FileShare.None);
StreamWriter sw = new StreamWriter(fs1);
sw.WriteLine(dataRemoved);
sw.Close();
但它的工作不正确 - 它必须删除到文件开头并删除所有的复制字符串;符号,我哪里错了?