Hello everyone!
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 really fast (non-assembly code).
Hello everyone!
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 really fast (non-assembly code).
File.ReadAllText(a) + File.ReadAllText(b) + File.ReadAllText(c)
string.Join("", Array.ConvertAll(new[] { "file1","file2", "file3" }, File.ReadAllText));
void CopyStream(Stream destination, Stream source) {
int count;
byte[] buffer = new byte[BUFFER_SIZE];
while( (count = source.Read(buffer, 0, buffer.Length)) > 0)
destination.Write(buffer, 0, count);
}
CopyStream(outputFileStream, fileStream1);
CopyStream(outputFileStream, fileStream2);
CopyStream(outputFileStream, fileStream3);
i don't know C# so well - can you call the shell from there? can you do something like
system("type file1 file2 file3 > out");
?
i believe that asymptotically, this ought to be very fast.
niko
Another way....how about letting the OS do it for you?:
ProcessStartInfo psi = new ProcessStartInfo("cmd.exe",
String.Format(@" /c copy {0} + {1} + {2} {3}",
file1, file2, file3, dest));
psi.UseShellExecute = false;
Process process = Process.Start(psi);
process.WaitForExit();
HTH
Kev
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, Marc