c# txt读取文档 读到某一行数据 怎么删掉

StreamReader msw = new StreamReader(file, System.Text.Encoding.Default);
string line;
while ((line = msw.ReadLine()) != null)
{
string[] str = line.Split(':', '\n');
if (str[0].ToString() == "1")
{
if (str[1] == null)
{ }
else
{
line.Remove(0);
}
line.Remove(0);这样写就有错误 怎么改啊

首先你这种写法是错误的,line.Remove(0);这只是要删除字符串中的数据,而非文件,相当于刻舟求剑,总体思路为删除一行后重写,这段代码是在你基础上改的,觉得不妥的地方可以自己修改一下再

            StreamReader msw = new StreamReader(@"D:\aaa.txt", System.Text.Encoding.Default);
            List<string> lines = new List<string>(File.ReadAllLines(@"D:\aaa.txt"));
            string line;
            int index = 0;
            while ((line = msw.ReadLine()) != null)
            {
                string[] str = line.Split(':', '\n');
                if (str[0].ToString() == "1")
                {
                    if (str[1] == null)
                    { }
                    else
                    {
                        lines.RemoveAt(index);
                    }
                }
                index++;
            }
            msw.Close();
            File.WriteAllLines(@"D:\aaa.txt", lines.ToArray());

温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-06-08
你可以替换成 “” 不就行了追问

不行吧

相似回答