Languages
[Edit]
EN

C# / .NET - get file size

5 points
Created by:
Roseanne-Read
1365

In this article, we're going to have a look at how to get file size in C# / .NET.

Quick solution:

 

Look at the below code to see practical usage example:

1. FileInfo class example

Edit

In the presented solution FileInfo class was used with Length property that returns file size.

Note: if file was modified after FileInfo object was created Refresh() method should be called on the object to get current file inforamtions.

Output:

2. File.Open method with Length property example

Edit

In some cases when we use Length property with specific stream, we need to be sure that the property is implemented - for File.Open method result it is.

Note: this approach is useful when we have opened stream.

Output:

References

Edit
  1. FileInfo.Length Property - Microsoft Docs
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

C# / .NET - file API

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join