![](/img/trans.png)
[英]I am new to C# selenium. I am trying to create log file using FileAppender in log4net.
[英]I am trying to create a new text file in C#, but the same information keeps populating
我正在尝试从已经存在的文本文件中创建一个新的文本文件。 使用streamreader和streamwriter解析整个文件并创建一个新文件来“吐出”我想要的数据的方法是什么。 我希望新数据文件以发票编号开头,该编号从 40 个字符开始,长度为 15 个字符 (40,15)。 在文本文件中,所有数据都用逗号分隔。 为此,我尝试应用 substring 方法(见下文)。
当我运行此代码时,该文件一直显示与原始文本文件相同的信息。 我认为这可能与我的循环方式有关,但我只是不知道。
非常感谢任何帮助或线索。
这就是我使用所有代码设置 Program.cs 文件的方式:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace TextFileDataAccessDemo
{
class Program
{
static void Main(string[] args)
{
string filePath = @"C:\MyData\test.txt";
List<string> lines = File.ReadAllLines(filePath).ToList();
//This loops over every line in the list of strings.
var newLines = lines.Select(l => l.Substring(40));
foreach (string line in lines)
{
Console.WriteLine(line);
}
File.WriteAllLines(filePath, lines);
Console.ReadLine();
}
static void MAin(string[] args)
{
string filePath = @"C:\MyData\test.txt";
List<string> lines = File.ReadAllLines(filePath).ToList();
string invoiceNumber = filePath.Substring(40, 15);
foreach (string line in lines)
{
Console.WriteLine(invoiceNumber);
}
Console.ReadKey();
}
}
}
编辑:字符串示例:2.1,00000556KV,0000E40E19,US,2016-02-27,000000E40E19096,
我想要什么:000000E40E19096,2016-02-27,US,0000E40E19,2.1,00000556KV
var newLines = lines.Select(l => l.Substring(40));
foreach (string line in lines)
{
Console.WriteLine(line);
}
您设置 newLines 然后从不使用它。 如果您将其更改为:
foreach (string line in newLines)
{
Console.WriteLine(line);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.