---
title: "How can you get the size of a file in bytes?"  
description: "How can you get the size of a file in bytes?"  
author: "ICSM Computer"  
published: 2025-05-07  
updated: 2025-05-21  
canonical: https://www.mindstick.com/forum/161584/how-can-you-get-the-size-of-a-file-in-bytes  
category: "c#"  
tags: ["c#", "file handling"]  
reading_time: 1 minute  

---

# How can you get the size of a file in bytes?

How can you get the [size](https://www.mindstick.com/forum/159799/how-can-you-manage-the-size-of-mdf-and-ldf-files) of a [file](https://www.mindstick.com/articles/59/encrypting-and-decrypting-files-using-c-sharp) in bytes? with example.

## Replies

### Reply by Utpal Vishwas

To get the **size of a file in bytes** in C#, you can use the `FileInfo` class from the `System.IO` namespace.

## Example: Get File Size in Bytes

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

class Program
{
    static void Main()
    {
        string filePath = @"C:\example\myfile.txt";

        if (File.Exists(filePath))
        {
            FileInfo fileInfo = new FileInfo(filePath);
            long sizeInBytes = fileInfo.Length;

            Console.WriteLine($"File size: {sizeInBytes} bytes");
        }
        else
        {
            Console.WriteLine("File does not exist.");
        }
    }
}
```

### Notes:

1. `FileInfo.Length` returns the size in **bytes** as a `long`.
2. Always check if the file exists to avoid exceptions.

### Convert to KB/MB (Optional):

```cs
double sizeInKB = sizeInBytes / 1024.0;
double sizeInMB = sizeInBytes / (1024.0 * 1024.0);
```


---

Original Source: https://www.mindstick.com/forum/161584/how-can-you-get-the-size-of-a-file-in-bytes

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
