Advertisement
vlad0

Text Files 03

Jan 23rd, 2013
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.62 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6.  
  7. namespace _03.EnterLineNumbers
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             string firstFileName = "../../mytemp.txt"; //the dir of the .cs file
  14.             string newFileName = "../../ConcatenatedFile.txt"; //the dir of the .cs file
  15.  
  16.             Archive(newFileName);
  17.  
  18.             AddLines(firstFileName, newFileName);
  19.  
  20.             PrintFile(newFileName);
  21.         }
  22.  
  23.         private static void PrintFile(string newFileName)
  24.         {
  25.             Console.Write("Do You want to read the concatenated file <y/n>: ");
  26.             char answer = (char)Console.Read();
  27.  
  28.             if (answer == 'y')
  29.             {
  30.                 using (StreamReader concatenated = new StreamReader(newFileName))
  31.                 {
  32.                     string line = concatenated.ReadLine();
  33.  
  34.                     while (line != null)
  35.                     {
  36.                         Console.WriteLine(line);
  37.                         line = concatenated.ReadLine();
  38.                     }
  39.                 }
  40.             }
  41.             else
  42.             {
  43.                 Console.WriteLine("GoodBye");
  44.             }
  45.         }
  46.  
  47.         private static void AddLines(string firstFileName, string newFileName)
  48.         {
  49.             using (StreamWriter destinationFile = new StreamWriter(newFileName))
  50.             {
  51.                 using (StreamReader sourceFile = new StreamReader(firstFileName))
  52.                 {
  53.                     string line = sourceFile.ReadLine();
  54.                     int countLine = 1;
  55.                     while (line != null)
  56.                     {
  57.                         destinationFile.Write("Line: {0} ", countLine);
  58.                         destinationFile.WriteLine(line);
  59.                         line = sourceFile.ReadLine();
  60.                         countLine++;
  61.                     }
  62.                 }
  63.             }
  64.  
  65.             Console.WriteLine("Manipulation ready!");
  66.         }
  67.  
  68.         private static void Archive(string newFileName)
  69.         {
  70.             if (File.Exists(newFileName)) //if the file already exists we make an archive
  71.             {
  72.                 string archiveName = newFileName.Remove(0, 6);
  73.                 string archive = "../../archive" + "-" + DateTime.Now.Hour + "-" + DateTime.Now.Day + "-" + DateTime.Now.Minute + "-" + DateTime.Now.Second + archiveName;
  74.                 File.Copy(newFileName, archive);
  75.                 File.Delete(newFileName);
  76.             }
  77.  
  78.             File.Create(newFileName).Close();
  79.         }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement