What would be the fastest way to concatenate three files in C#?

后端 未结 7 1790
滥情空心
滥情空心 2020-11-29 07:40

I need to concatenate 3 files using C#. A header file, content, and a footer file, but I want to do this as cool as it can be done.

Cool = really small code or reall

7条回答
  •  南笙
    南笙 (楼主)
    2020-11-29 08:10

    You mean 3 text files?? Does the result need to be a file again?

    How about something like:

    string contents1 = File.ReadAllText(filename1);
    string contents2 = File.ReadAllText(filename2);
    string contents3 = File.ReadAllText(filename3);
    
    File.WriteAllText(outputFileName, contents1 + contents2 + contents3);
    

    Of course, with a StringBuilder and a bit of extra smarts, you could easily extend that to handle any number of input files :-)

    Cheers

提交回复
热议问题