将所有行读入内存
var lines =File.ReadAllLines(args[0]);
using (var outfs = File.AppendText(args[1]))
{
outfs.Write("<html><body><table>");
foreach (var line in lines)
outfs.Write("<tr><td>" + string.Join("</td><td>", line.Split(',')) + "</td></tr>");
outfs.Write("</table></body></html>");
}
或一次读一行
using (var inFs = File.OpenText(args[0]))
using (var outfs = File.AppendText(args[1]))
{
outfs.Write("<html><body><table>");
while (!inFs.EndOfStream )
outfs.Write("<tr><td>" + string.Join("</td><td>", inFs.ReadLine().Split(',')) + "</td></tr>");
outfs.Write("</table></body></html>");
}
... @Jimmy ...我使用 LINQ 创建了一个扩展版本。这是亮点......(用于行阅读的懒惰评估)
using (var lp = args[0].Load())
lp.Select(l => "<tr><td>" + string.Join("</td><td>", l.Split(',')) + "</td></tr>")
.Write("<html><body><table>", "</table></body></html>", args[1]);