Advertisement
Guest User

Untitled

a guest
Jun 13th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.93 KB | None | 0 0
  1. private static void CreateOtherFiles(List<string> listOfLines, int maxLinesPerFile, string path)
  2.         {
  3.             int numOfFiles = listOfLines.Count / maxLinesPerFile;
  4.             if (listOfLines.Count % maxLinesPerFile != 0)
  5.             {
  6.                 numOfFiles++;
  7.             }
  8.  
  9.             int currentLocationInList = 0;
  10.  
  11.             for (int i = 1; i <= numOfFiles; i++)
  12.             {
  13.                 try
  14.                 {
  15.                     path = Path.GetFileNameWithoutExtension(path);
  16.                     using (StreamWriter sw = new StreamWriter(Path.Combine(Environment.CurrentDirectory, path + "-" + i + ".txt")))
  17.                     {
  18.                         for (int j = 0; j < maxLinesPerFile; j++)
  19.                         {
  20.                             if (listOfLines.Count - currentLocationInList > 0)
  21.                             {
  22.                                 sw.WriteLine(listOfLines[currentLocationInList]);
  23.                                 currentLocationInList++;
  24.                             }
  25.                             else
  26.                             {
  27.                                 break;
  28.                             }
  29.                         }
  30.                     }
  31.                 }
  32.                 catch (IOException ex)
  33.                 {
  34.                     Console.WriteLine("There was an error writing to file " + ex.Message);
  35.                 }
  36.             }
  37.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement