ttmind

Main Navigation

ttmind
  • jim-jams
  • Tech
  • Positive
Login

Login

Facebook Google

OR

Remember me Forgot password?

Don't have account? Signup here.

Sort by Categorys

.Net

PHP

Java

JavaScript

Database

Server

Client Side

Tools

Artificial Intelligence

Cloud

Hybrid Development

Event

Smart City

Education

Security

Scrum

Digital Marketing

APP Development

Business

Internet

Simulation

Art

Network

Microservices

Architecture

Technology

Leadership

    Top Articles

  • How Does Social Media Bring People Together?
    TTMind Author
  • How to read appSettings JSON from Class Library in ASP.NET Core
    Anil Shrestha
  • Printing Support In Asp.Net Core
    TTMind Author
  • HOW TO EXTRACT TEXT FROM IMAGE USING JAVASCRIPT (OCR with Tesseract.js)?
    Prakash Pokhrel
  • Images Upload REST API using ASP.NET Core
    Prakash Pokhrel
  • Related Topic

  • How to read appSettings JSON from Class Library in ASP.NET Core
  • Printing Support In Asp.Net Core
  • Images Upload REST API using ASP.NET Core
  • How to use IActionFilter, IAsyncActionFilter in ASP.NET Core MVC?
  • ASP.NET CORE - Blazor CRUD operation using ADO.NET
  • Tech
  • About Us
  • Contact Us
  • TechHelp
  • PositiveHelp
  • Jim-Jams Help
  • Terms & Conditions

© Copyright ttmind.com

Main Content

Get file size in bytes in C# using the FileInfo.Length property

.Net C# about 6 years ago || 11/24/2019 || 6.1 K View

  • Hide

Get File Size in C#.

How to get a file size in bytes using C#  using the FileInfo.Length property.

The size of any file in bytes can be returned by the Length property of FileInfo Class. Following code returns the size of file:

// To Get size of any file  
long FileSize = MyFile.Length;  
Console.WriteLine("File Size in Bytes: {0}", FileSize); 

Here, to run it we must import System.IO and System.Text namespaces in the project.

Coding Implementation

Here is the complete coding implementation. Here we use FileInfo class to create an object by passing a complete File location with its name. FileInfo class allows us to get information about a file. Such as: file name, size, full path, extension, directory name, is file read only or not, File creation date, updated date etc.

Note:  we can convert the size from bytes to KB, MB, and GB by dividing bytes by 1024, 1024x1024 and so on. 

using System;
using System.IO;
using System.Text;
namespace FileSize
{
    class Program
    {
        static void Main(string[] args)
        {
            // Full file name   
            string filePath = @"C:\Users\SIW\Documents\TestFile.txt";
            FileInfo MyFile = new FileInfo(filePath);
            // Create a new file   
            using (FileStream fs = MyFile.Create())
            {
                Byte[] txt = new UTF8Encoding(true).GetBytes("New file.");
                fs.Write(txt, 0, txt.Length);
                Byte[] author = new UTF8Encoding(true).GetBytes("Author TTMind Community");
                fs.Write(author, 0, author.Length);
            }

            // Get File Name  
            string justFileName = MyFile.Name;
            Console.WriteLine("File Name: {0}", justFileName);
            // Get file name with full path   
            string fullFileName = MyFile.FullName;
            Console.WriteLine("File Name: {0}", fullFileName);
            // Get file extension   
            string extn = MyFile.Extension;
            Console.WriteLine("File Extension: {0}", extn);
            // Get directory name   
            string directoryName = MyFile.DirectoryName;
            Console.WriteLine("Directory Name: {0}", directoryName);
            // File Exists ?  
            bool exists = MyFile.Exists;
            Console.WriteLine("File Exists: {0}", exists);
            if (MyFile.Exists)
            {
                //To Get Size of any file
                long size = MyFile.Length;
                Console.WriteLine("File Size in Bytes: {0}", size);
                //is our File ReadOnly ?  
                bool IsReadOnly = MyFile.IsReadOnly;
                Console.WriteLine("Is ReadOnly: {0}", IsReadOnly);
                //get Creation, last access, and last write time   
                DateTime creationTime = MyFile.CreationTime;
                Console.WriteLine("Creation time: {0}", creationTime);
                DateTime accessTime = MyFile.LastAccessTime;
                Console.WriteLine("Last access time: {0}", accessTime);
                DateTime updatedTime = MyFile.LastWriteTime;
                Console.WriteLine("Last write time: {0}", updatedTime);
            }
        }
    }
}

Output:

  • 0
  • 0
  • 0
    • Facebook
    • Twitter
    • Google +
    • LinkedIn

About author

Anthony M. Healey

Anthony M. Healey

Touch of Creativity and a bit of active on brain power

Reset Your Password
Enter your email address that you used to register. We'll send you an email with your username and a link to reset your password.

Quick Survey