Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- string sourceFileName = "source.txt";
- string destinationFileName = "File-Part-{0}.txt";
- int linesPerFile = 3;
- using (var sourceFile = new StreamReader(sourceFileName))
- {
- var fileCounter = 0;
- var destinationFile = new StreamWriter(
- string.Format(destinationFileName, fileCounter + 1));
- try
- {
- var lineCounter = 0;
- string line;
- while ((line = sourceFile.ReadLine()) != null)
- {
- if (lineCounter >= linesPerFile)
- {
- lineCounter = 0;
- fileCounter++;
- destinationFile.Dispose();
- destinationFile = new StreamWriter(
- string.Format(destinationFileName, fileCounter + 1));
- }
- destinationFile.WriteLine(line);
- lineCounter++;
- }
- }
- finally
- {
- destinationFile.Dispose();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement