C# 跳过文本文件的第一行

标签 c#

这是我正在使用的代码

        using (FileStream fs = new FileStream(filename, FileMode.Open))
        using (StreamReader rdr = new StreamReader(fs))
        {
            while (!rdr.EndOfStream)
            {
                for (int z = 0; z < 2; z++)
                {

                    string[] lines = rdr.ReadLine().Split('|');
                    {
                        sb.AppendLine(";Re");
                        sb.AppendLine("@C PAMT " + lines[3]);
                        sb.AppendLine("@T " + lines[0]);
                        sb.AppendLine("@D @I\\" + lines[1]).Replace("I:\\", "");
                        sb.AppendLine(lines[2].Replace(";", "\r\n");
                    }
                }
            }
        }

        using (FileStream fs = new FileStream(outputfilename, FileMode.Create))
        using (StreamWriter writer = new StreamWriter(fs))
        {
            writer.Write(sb.ToString());
        }

我只想跳过 StreamReader 的第一行或跳过整个第一个 stringbuilder。我以为 for 会这样做,但事实并非如此。

注意:我想避免使用 foreach。

最佳答案

为什么不在创建 rdr 变量后立即添加以下行?

if ( !rdr.EndOfStream ) {rdr.ReadLine();}

关于C# 跳过文本文件的第一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2134700/

相关文章:

c# - 如何在运行时更新 Autofac 容器中的注册实例

c# - 如何将 reader.Read 字符串转换为 C# 中的货币格式?

c# - 响应网络代码调用函数

c# - 如何在C#程序中获取任务栏通知区域宽度?

c# - .Net core将SqlConnection注入(inject)到服务中

c# - 从数据库表中删除行

C# 遍历并设置结构中的所有字段

c# - 使用 LINQ for Dropdown 返回串联字符串

c# - 从 C# 调用 C++/CLI 代码

c# - 底层连接已关闭 : The connection was closed unexpectedly. 数据太大?