ASP.Net Get File Size

If you want to get the size of a file in ASP.NET (C#), do this:

string MyFile = "~/photos/mymug.gif";

FileInfo finfo = new FileInfo(Server.MapPath(MyFile));
long FileInBytes = finfo.Length;
long FileInKB = finfo.Length / 1024;

Response.Write("File Size: " + FileInBytes.ToString() + 
  " bytes (" + FileInKB.ToString() + " KB)");

19 Comments

Leandro ArdissoneJanuary 5th, 2006 at 12:54 am

Thanks..

useful.. :)

SeshadriJanuary 26th, 2006 at 11:01 pm

Thank you very much
its very usefuly for me

RanjithMarch 18th, 2006 at 11:05 am

Thanks for ur solution

Regards
Ranjith

AjayOctober 20th, 2006 at 1:09 pm

Thanks dude. I got a straight forward answer.

mart manJanuary 31st, 2007 at 8:01 pm

Thanks dude, just what i was looking for. i needed it in vb so i converted it.

Hope the code helps some others

Dim finfo As FileInfo = New FileInfo(fileName)
Dim FileInBytes As Integer = finfo.Length

Nimish V. GorayMarch 17th, 2007 at 7:06 pm

Thanks a lot Sir, this was utmost helpful…

BrundabanJuly 10th, 2007 at 12:03 pm

Thanks a lot, this is solve my problem

SkySeptember 14th, 2007 at 11:56 am

Straight and direct answer to what i need. Thanx Man..

Ashish TayalNovember 15th, 2007 at 12:13 pm

Thanks a lot.
Help me a lot.

HafeezDecember 17th, 2007 at 4:49 pm

Thanks a lot, very much useful

RoloMarch 18th, 2008 at 12:00 am

Just what I needed. Thanks.

MilanOctober 3rd, 2008 at 10:56 am

It helps me a lot. Thanks

Matt S.December 10th, 2008 at 3:25 am

I’m getting the following error:

System.Web.HttpException: ‘http://www.example.com’ is not a valid virtual path.

I didn’t really use example.com, but it doesn’t recognize any website I plug in there. I even tried linking to an image, and the image wasn’t “a valid virtual path” either. What could the problem be?

LacunaApril 27th, 2009 at 8:55 pm

Works perfectly. Thanks :)

Mohit GoelApril 30th, 2009 at 1:52 pm

Thanks,
Really helped me…

VJune 19th, 2009 at 5:26 pm

thanks man

JasiJune 9th, 2010 at 8:06 pm

Thanka a lot..

DhivyaJune 30th, 2010 at 12:47 pm

Thanks for your code.
It is very useful to me.
grt…

DarshanFebruary 7th, 2011 at 1:52 pm

thnks

Leave a comment

Your comment