ellapt

T13.2.Concat2files

Jan 31st, 2013
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3.  
  4. class Concat2files
  5. {
  6. static void MoveFile(string inpFile,string resFile)
  7. {
  8. string fileContents = "";
  9. try
  10. {
  11. StreamReader reader = new StreamReader(inpFile);
  12. fileContents = reader.ReadToEnd();
  13. reader.Close();
  14. }
  15. catch (Exception ex)
  16. {
  17. Console.WriteLine("Error Reading file {0}: {1}", inpFile, ex.Message);
  18. }
  19. try
  20. {
  21. StreamWriter streamWriter = new StreamWriter(resFile,true);
  22. streamWriter.Write(fileContents);
  23. streamWriter.Close();
  24. }
  25. catch (Exception ex)
  26. {
  27. Console.WriteLine("Error Writing file {0}: {1}", resFile, ex.Message);
  28. }
  29. finally
  30. {
  31. Console.Write("{0}\n", inpFile);
  32. }
  33. }
  34.  
  35. static void Main()
  36. {
  37. Console.Write("Concatenate text files:\n");
  38. string fileName1 = @"../../Readme.txt";
  39. string fileName2 = @"../../License.txt";
  40. string concatFileName = @"../../ReadmeLicense.txt";
  41. MoveFile(fileName1,concatFileName);
  42. MoveFile(fileName2,concatFileName);
  43.  
  44. try
  45. {
  46. Console.WriteLine("to another text file: {0} \n", concatFileName);
  47. StreamReader reader = new StreamReader(concatFileName);
  48. using (reader)
  49. {
  50. string line = reader.ReadLine();
  51. while (line != null)
  52. {
  53. Console.WriteLine(line);
  54. line = reader.ReadLine();
  55. }
  56. }
  57. }
  58. catch (Exception ex)
  59. {
  60. Console.WriteLine("Error Reading file {0}: {1}", concatFileName, ex.Message);
  61. }
  62. finally
  63. {
  64. Console.WriteLine("Please, scroll up to observe the result.");
  65. }
  66. }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment