打开txt文件并替换前两行的文本(c#)

本文关键字:两行 txt 文本 替换 打开 文件 | 更新日期: 2023-09-27 18:15:33

我正在编写这个程序,它允许我生成带有增量数字的txt文件,但是,我希望每个文件序列号都写入txt文件本身。

例如

:我生成了3个文件,Mytext-000001.txt, Mytext-000002.txt, Mytext-000003.txt,每个文件的第一行包含"Hello 000000",第二行包含"My number is 000000",现在我想将每个txt文件更改为包含"Hello " +其命名的增量数字。

所以每个文件的输出将是:

Mytext-000001.txt,
Hello 000001
My number is 000001
Mytext-000002.txt,
Hello 000002
My number is 000002

Mytext-000003.txt,
Hello 000002
My number is 000003

我的代码

string path = System.IO.Path.GetDirectoryName(Application.ExecutablePath) + @"'path.txt";
string pth_input = null;
string pth_output = null;
using (StreamReader sx = File.OpenText(path))
{
    pth_input = sx.ReadLine();
    pth_output = sx.ReadLine();
}

Console.WriteLine("Number of Files?");
string number_of_files = Console.ReadLine();
int get_number_of_files = Int32.Parse(number_of_files) + 1;
string PathFiletoCopy = pth_input;
string Extension = System.IO.Path.GetExtension(PathFiletoCopy);
string PartialNewPathFile = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(PathFiletoCopy), System.IO.Path.GetFileNameWithoutExtension(PathFiletoCopy) + "-");
for (int i = 1; i < get_number_of_files; i++)
{
    System.IO.File.Copy(PathFiletoCopy, PartialNewPathFile + i.ToString("D6") + Extension);
}
string[] txtfiles = Directory.GetFiles(pth_output, "*.txt");
foreach (var file in txtfiles)
{
    string get_file_counter = System.IO.Path.GetDirectoryName(file.Substring(7,6));
    FileStream fs = new FileStream(file, FileMode.Append, FileAccess.Write);
    FileStream fi = new FileStream(file, FileMode.Open, FileAccess.Read);
    using (StreamReader reader = new StreamReader(fi))
    {
        using (StreamWriter writer = new StreamWriter(fs))
        {
            string line = null;
            while ((line = reader.ReadLine()) != null)
            {
             string replace_line_one = line.Replace("Hello 000001","Hello"+ "["+get_file_counter+"]");
             string replace_line_two = line.Replace("My number is 000001", "My number is" + "[" + get_file_counter + "]");
            }
            writer.Close();
        } reader.Close();
    } 
}
Console.Read();

我希望你能帮助我

感谢大家的帮助

打开txt文件并替换前两行的文本(c#)

string[] files = Directory.GetFiles(directoryPath, "*.txt");
Regex regex = new Regex("''d+(?=''.txt)");
foreach (var file in files)
{
    string[] lines = File.ReadAllLines(file);
    string number = regex.Match(Path.GetFileName(file)).Value;
    lines[0] = "Hello " + number;
    lines[1] = "My number is " + number;
    File.WriteAllLines(file, lines);
}

Regex使此解决方案非特定。

这可能对您有用

System.IO.Directory myDir = pth_output;
int count = (myDir.GetFiles().Length) + 1;
string thenumber = String.Format("0:000000", count);
string filename = "Mytext-" + thenumber + ".txt";
string filetext = "Hello " + thenumber + Environment.NewLine + "My number is " + thenumber;
File.WriteAllText(Path.Combine(myDir,filename) , createText);

myDir中,我希望您可以拉出包含所有txt文件的文件夹的路径。

myDir.GetFiles().Length将给你文件夹中存在的文件的数量,因为我们只想要txt文件,你可以像Directory.GetFiles(path, "*.txt", SearchOption.AllDirectories).Length;一样搜索它,而不是

String.Format("0:000000", count);将以前面的零的格式给出数字。