w3resource

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

C# Sharp Basic: Exercise-29 with Solution

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

C# Sharp Code Editor:

Previous: Write a C# program to reverse the words of a sentence.
Next: Write a C# program to convert a hexadecimal number to decimal number.

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.