---
title: "How do you split a large file into multiple smaller files based on size?"  
description: "How do you split a large file into multiple smaller files based on size?"  
author: "ICSM Computer"  
published: 2025-05-16  
updated: 2025-05-16  
canonical: https://www.mindstick.com/interview/34128/how-do-you-split-a-large-file-into-multiple-smaller-files-based-on-size  
category: "c#"  
tags: ["c#", "file handling"]  
reading_time: 3 minutes  

---

# How do you split a large file into multiple smaller files based on size?

To **split a large file into multiple smaller files based on size** in C#, you can:

1. Read the large file in chunks.
2. Write each chunk to a new file when the size limit is reached.

### Example: Split File by Size in MB

```cs
using System;
using System.IO;

class FileSplitter
{
    public static void SplitFile(string inputFile, string outputDir, int maxSizeInMB)
    {
        const int bufferSize = 1024 * 1024; // 1 MB
        byte[] buffer = new byte[bufferSize];
        int partNumber = 1;
        long maxBytesPerFile = maxSizeInMB * 1024L * 1024L;

        using FileStream sourceStream = new FileStream(inputFile, FileMode.Open, FileAccess.Read);
        Directory.CreateDirectory(outputDir);

        while (sourceStream.Position < sourceStream.Length)
        {
            string partFileName = Path.Combine(outputDir, $"{Path.GetFileNameWithoutExtension(inputFile)}.part{partNumber}{Path.GetExtension(inputFile)}");
            using FileStream partStream = new FileStream(partFileName, FileMode.Create, FileAccess.Write);

            long bytesWritten = 0;
            while (bytesWritten < maxBytesPerFile && sourceStream.Position < sourceStream.Length)
            {
                int bytesToRead = (int)Math.Min(bufferSize, maxBytesPerFile - bytesWritten);
                int bytesRead = sourceStream.Read(buffer, 0, bytesToRead);
                partStream.Write(buffer, 0, bytesRead);
                bytesWritten += bytesRead;
            }

            Console.WriteLine($"Created: {partFileName}");
            partNumber++;
        }
    }
}
```

### Usage

```cs
FileSplitter.SplitFile("largefile.zip", "output", 10); // Split into 10 MB parts
```

### Notes

1. This example uses **binary splitting**, suitable for any file type.
2. Files are named like `largefile.part1.zip`, `largefile.part2.zip`, etc.
3. To **merge** them later, you'd read them in order and write to a single file.

## Read More

1. [How do you log all file system operations (create, read, write, delete) in an application?](https://www.mindstick.com/interview/34126/how-do-you-log-all-file-system-operations-create-read-write-delete-in-an-application)
2. [How do you serialize and save an object to a binary file?](https://www.mindstick.com/forum/161626/how-do-you-serialize-and-save-an-object-to-a-binary-file)
3. [How can you throttle file read/write speed to simulate slow I/O conditions?](https://www.mindstick.com/forum/161625/how-can-you-throttle-file-read-write-speed-to-simulate-slow-i-o-conditions)

## Answers

### Answer by ICSM Computer

To **split a large file into multiple smaller files based on size** in C#, you can:

1. Read the large file in chunks.
2. Write each chunk to a new file when the size limit is reached.

### Example: Split File by Size in MB

```cs
using System;
using System.IO;

class FileSplitter
{
    public static void SplitFile(string inputFile, string outputDir, int maxSizeInMB)
    {
        const int bufferSize = 1024 * 1024; // 1 MB
        byte[] buffer = new byte[bufferSize];
        int partNumber = 1;
        long maxBytesPerFile = maxSizeInMB * 1024L * 1024L;

        using FileStream sourceStream = new FileStream(inputFile, FileMode.Open, FileAccess.Read);
        Directory.CreateDirectory(outputDir);

        while (sourceStream.Position < sourceStream.Length)
        {
            string partFileName = Path.Combine(outputDir, $"{Path.GetFileNameWithoutExtension(inputFile)}.part{partNumber}{Path.GetExtension(inputFile)}");
            using FileStream partStream = new FileStream(partFileName, FileMode.Create, FileAccess.Write);

            long bytesWritten = 0;
            while (bytesWritten < maxBytesPerFile && sourceStream.Position < sourceStream.Length)
            {
                int bytesToRead = (int)Math.Min(bufferSize, maxBytesPerFile - bytesWritten);
                int bytesRead = sourceStream.Read(buffer, 0, bytesToRead);
                partStream.Write(buffer, 0, bytesRead);
                bytesWritten += bytesRead;
            }

            Console.WriteLine($"Created: {partFileName}");
            partNumber++;
        }
    }
}
```

### Usage

```cs
FileSplitter.SplitFile("largefile.zip", "output", 10); // Split into 10 MB parts
```

### Notes

1. This example uses **binary splitting**, suitable for any file type.
2. Files are named like `largefile.part1.zip`, `largefile.part2.zip`, etc.
3. To **merge** them later, you'd read them in order and write to a single file.

## Read More

1. [How do you log all file system operations (create, read, write, delete) in an application?](https://www.mindstick.com/interview/34126/how-do-you-log-all-file-system-operations-create-read-write-delete-in-an-application)
2. [How do you serialize and save an object to a binary file?](https://www.mindstick.com/forum/161626/how-do-you-serialize-and-save-an-object-to-a-binary-file)
3. [How can you throttle file read/write speed to simulate slow I/O conditions?](https://www.mindstick.com/forum/161625/how-can-you-throttle-file-read-write-speed-to-simulate-slow-i-o-conditions)


---

Original Source: https://www.mindstick.com/interview/34128/how-do-you-split-a-large-file-into-multiple-smaller-files-based-on-size

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
