Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.25 KB | None | 0 0
  1.  string sourceFileName = "source.txt";
  2.             string destinationFileName = "File-Part-{0}.txt";
  3.             int linesPerFile = 3;
  4.  
  5.             using (var sourceFile = new StreamReader(sourceFileName))
  6.             {
  7.                 var fileCounter = 0;
  8.                 var destinationFile = new StreamWriter(
  9.                     string.Format(destinationFileName, fileCounter + 1));
  10.  
  11.                 try
  12.                 {
  13.                     var lineCounter = 0;
  14.  
  15.                     string line;
  16.                     while ((line = sourceFile.ReadLine()) != null)
  17.                     {
  18.                         if (lineCounter >= linesPerFile)
  19.                         {
  20.                             lineCounter = 0;
  21.                             fileCounter++;
  22.  
  23.                             destinationFile.Dispose();
  24.                             destinationFile = new StreamWriter(
  25.                                 string.Format(destinationFileName, fileCounter + 1));
  26.                         }
  27.  
  28.                         destinationFile.WriteLine(line);
  29.                         lineCounter++;
  30.                     }
  31.                 }
  32.                 finally
  33.                 {
  34.                     destinationFile.Dispose();
  35.                 }
  36.             }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement