File.Split Method

Splits the file as many.
public static void Split( 
string fileInputPath
string folderOutputPath
int countOfOutputFiles 
)
This language is not supported or no code example is available.

Parameters

fileInputPath
string

The file input path.

folderOutputPath
string

The folder output path.

countOfOutputFiles
int

The count of output files.

public static void Split(string fileInputPath,
     string folderOutputPath,
     int countOfOutputFiles)
 {
     byte[] byteSource = System.IO.File.ReadAllBytes(fileInputPath);
     FileInfo fiSource = new FileInfo(fileInputPath);
     int partSize = (int)Math.Ceiling((double)(fiSource.Length / countOfOutputFiles));
     int fileOffset = 0;
     string currPartPath;
     FileStream fsPart;
     int sizeRemaining = (int)fiSource.Length;
     for (int i = 0; i < countOfOutputFiles; i++)
     {
         currPartPath = folderOutputPath + "\\" + fiSource.Name + "." + string.Format(@"{0:D4}", i) + ".part";
         if (!System.IO.File.Exists(currPartPath))
         {
             fsPart = new FileStream(currPartPath, FileMode.CreateNew);
             sizeRemaining = (int)fiSource.Length - (i * partSize);
             if (sizeRemaining < partSize)
                 partSize = sizeRemaining;
             fsPart.Write(byteSource,
                 fileOffset,
                 partSize);
             fsPart.Close();
             fileOffset += partSize;
         }
     }
 }
					
This language is not supported or no code example is available.

.NET Framework

Supported in: 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8

.NET Core

Supported in: 5.0+, 6.0+

In this article

Definition