Friday, July 27, 2012

Convert file size bytes to megabyte string using c#

Recently I needed to convert a number of bytes to a readable string that represents the number of megabytes or gigabytes.
I created an extension method for FileInfo.Length property being a double. I also changed my method for toDouble to call the other method so that it can be used for both double and long.

FileUpload uploadImageFileControl = (divmain.FindControl("UploadImageFile") as FileUpload);double fileSize = (Convert.ToDouble(uploadImageFileControl.FileBytes.Length.ToString()) / 1024);double fileSize1 = (fileSize / 1024);string fileSizeInMb = (String.Format("{0:0.00}", fileSize1) + "MB");//it will  generate the format of 0.00MB

No comments: