C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Validate File Size In C#
WhatsApp
Faisal Pathan
5y
43.1
k
0
2
25
Blog
To achieve this, we can use the following namespace: "System.IO". Here is a custom code snippet, where the single parameter is the filePath parameter which is for accepting file path to validate size.
public
void
ValidateFileSize(
string
filePath) {
if
(!File.Exists(filePath)) {
//Throw error if file not found
}
FileInfo fileInfo =
new
FileInfo(filePath);
//Validate Length
if
(fileInfo.Length / 1048 > 20) {
//Throw error if file size is larger than your default/set size.
}
}
Related tutorial
Detailed FileInfo Tutorial In C#
ASP.NET MVC 5 - Limit Upload File Size
File Info
File Size
Validate File Size In C#
People also reading
Membership not found