w3resource

C#: Find the size of a specified file in bytes


File Size in Bytes

Write a C# program to find the size of a specified file in bytes.

C# Sharp Exercises: Find the size of a specified file in bytes

Sample Solution:-

C# Sharp Code:

using System;
using System.Collections.Generic;
using System.IO; // Importing the System.IO namespace for file handling

// This is the beginning of the Exercise29 class
public class Exercise29 {
    // This is the main method where the program execution starts
    public static void Main() {
        // Creating a FileInfo object representing a file at the specified path
        FileInfo f = new FileInfo("/home/students/abc.txt");

        // Displaying the size of the file in bytes using FileInfo's Length property
        Console.WriteLine("\nSize of a file: " + f.Length.ToString());
    }
}

Sample Output:

Size of a file: 31 

Flowchart:

Flowchart: C# Sharp Exercises - Find the size of a specified file in bytes

For more Practice: Solve these Related Problems:

  • Write a C# program to calculate the combined size of all files in a directory in bytes.
  • Write a C# program to determine the size in bytes of a text file excluding whitespace characters.
  • Write a C# program to compress a text file and show its size before and after compression.
  • Write a C# program to read a file and report its size in kilobytes and megabytes as well.

Go to:


PREV : Reverse Words in Sentence.
NEXT : Hexadecimal to Decimal.

C# Sharp Code Editor:



What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.