w3resource

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

C# Sharp Basic: Exercise-29 with Solution

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

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.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/csharp-exercises/basic/csharp-basic-exercise-29.php