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

时间:2016-10-31 01:20:36

标签: c# visual-studio

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

例如: 我生成了3个文件,Mytext-000001.txt,Mytext-000002.txt,Mytext-000003.txt,每个文件的第一行包含“Hello 000000”,第二行包含“我的号码是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();

我希望你能帮忙

感谢你的帮助人员

2 个答案:

答案 0 :(得分:0)

这可能会为你做到这一点

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);会以前面的零格式为您提供数字。

答案 1 :(得分:0)

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使这个解决方案非特异性。