//Create an object of FileInfo like :-
FileInfo fileObject = new FileInfo(selectedFilename);
Int64 fileSize = 0;
float sizeOfFile = 0
fileSize = fileObject.Length;
sizeOfFile = fileObject.Length;
private const int FILESIZE_IN_KB = 1024;
public const int FILESIZE_IN_MB = 1048576;
public const int FILESIZE_IN_GB = 1073741824;
public const long FILESIZE_IN_TB = 1099511627776;
private const int START_INDEX = 0;
private const string STRING_VALUE_ZERO = "0";
private const string STRING_VALUE_ONE = "1";
private const string SYMBOL_BYTES = "bytes";
private const string STRING_FORMAT = "n";
public const string FILESIZE_KB = "KB";
private const int FILE_SIZE_LIMIT = 100;
private const string STRING_FORMAT_UPTO_TWO_DECIMAL = ".00";
private const int FILE_SIZE_IN_BYTES = 102400;
private const int FILE_SIZE_IN_MegaBytes = 104857600;
private const long FILE_SIZE_IN_GIGABYTES = 107374182400;
private const string SYMBOL_SPACE = " ";
string size = FormatSize(fileSize,sizeOFFile);
private string FormatSize(Int64 fileSize, float sizeOFFile)
{
//Format number to KB
string stringSize = string.Empty;
NumberFormatInfo numberFormatInfo = new NumberFormatInfo();
Int64 fileSizeInKB = 0;
Int64 fileSizeInMB = 0;
long fileSizeInGB = 0;
float fileSizeInKBInFloat = 0;
float fileSizeInMBInFloat = 0;
float fileSizeInGBInFloat = 0;
string byteSize = string.Empty;
if (fileSize < FILESIZE_IN_KB)
{
if (fileSize == 0)
{
//zero byte
stringSize = STRING_VALUE_ZERO + SYMBOL_SPACE + SYMBOL_BYTES;
}
else
{
//less than 1K but not zero byte
stringSize = fileSize + SYMBOL_SPACE + SYMBOL_BYTES + SYMBOL_SPACE + SYMBOL_OPENING_BRACE + fileSize + SYMBOL_SPACE + SYMBOL_BYTES + SYMBOL_CLOSING_BRACE;
}
}
else if(fileSize < FILESIZE_IN_MB)
{
if (fileSize < FILE_SIZE_IN_BYTES)
{
fileSizeInKBInFloat = (sizeOFFile / (float)FILESIZE_IN_KB);
}
else
{
//convert to KB
fileSizeInKB = (fileSize / FILESIZE_IN_KB);
}
if (fileSizeInKBInFloat != 0)
{
stringSize = fileSizeInKBInFloat.ToString(STRING_FORMAT, numberFormatInfo);
}
else
{
//format number with default format
stringSize = fileSizeInKB.ToString(STRING_FORMAT, numberFormatInfo);
///If file Size is morethan 100 than replace decimal string to emptyString.
if (fileSizeInKB >= FILE_SIZE_LIMIT)
{
stringSize = stringSize.Replace(STRING_FORMAT_UPTO_TWO_DECIMAL, string.Empty);
}
}
///Sets the byteSize like 12345 into 12,345.
byteSize = fileSize.ToString(STRING_FORMAT, numberFormatInfo);
byteSize = byteSize.Replace(STRING_DECIMAL, string.Empty);
stringSize = stringSize + "KB" + SYMBOL_SPACE + SYMBOL_OPENING_BRACE + byteSize + SYMBOL_SPACE + SYMBOL_BYTES + SYMBOL_CLOSING_BRACE;
}
else if (fileSize < Constants.FILESIZE_IN_GB)
{
if (fileSize < FILE_SIZE_IN_MegaBytes)
{
fileSizeInMBInFloat = (sizeOFFile / (float)FILESIZE_IN_MB);
}
else
{
//convert to KB
fileSizeInMB = (fileSize / FILESIZE_IN_MB);
}
if (fileSizeInMBInFloat != 0)
{
stringSize = fileSizeInMBInFloat.ToString(STRING_FORMAT, numberFormatInfo);
}
else
{
//format number with default format
stringSize = fileSizeInMB.ToString(STRING_FORMAT, numberFormatInfo);
if (fileSizeInMB >= FILE_SIZE_LIMIT)
{
stringSize = stringSize.Replace(STRING_FORMAT_UPTO_TWO_DECIMAL, string.Empty);
}
}
///Sets the byteSize like 12345 into 12,345.
byteSize = fileSize.ToString(STRING_FORMAT, numberFormatInfo);
byteSize = byteSize.Replace(STRING_FORMAT_UPTO_TWO_DECIMAL, string.Empty);
stringSize = stringSize + "MB" + SYMBOL_SPACE + SYMBOL_OPENING_BRACE + byteSize +SYMBOL_SPACE + SYMBOL_BYTES + SYMBOL_CLOSING_BRACE;
}
else if(fileSize < FILESIZE_IN_TB)
{
if (fileSize < FILE_SIZE_IN_GIGABYTES)
{
fileSizeInGBInFloat = (sizeOFFile / (float)FILESIZE_IN_GB);
}
else
{
//convert to KB
fileSizeInGB = (fileSize / FILESIZE_IN_GB);
}
if (fileSizeInGBInFloat != 0)
{
stringSize = fileSizeInGBInFloat.ToString(STRING_FORMAT, numberFormatInfo);
}
else
{
//format number with default format
stringSize = fileSizeInGB.ToString(STRING_FORMAT, numberFormatInfo);
if (fileSizeInGB >= FILE_SIZE_LIMIT)
{
stringSize = stringSize.Replace(STRING_FORMAT_UPTO_TWO_DECIMAL, string.Empty);
}
}
///Sets the byteSize like 12345 into 12,345.
byteSize = fileSize.ToString(STRING_FORMAT, numberFormatInfo);
byteSize = byteSize.Replace(STRING_FORMAT_UPTO_TWO_DECIMAL, string.Empty);
stringSize = stringSize + "GB" + SYMBOL_SPACE + SYMBOL_OPENING_BRACE + byteSize + SYMBOL_SPACE + SYMBOL_BYTES + SYMBOL_CLOSING_BRACE;
}
return stringSize;
}
FileInfo fileObject = new FileInfo(selectedFilename);
Int64 fileSize = 0;
float sizeOfFile = 0
fileSize = fileObject.Length;
sizeOfFile = fileObject.Length;
private const int FILESIZE_IN_KB = 1024;
public const int FILESIZE_IN_MB = 1048576;
public const int FILESIZE_IN_GB = 1073741824;
public const long FILESIZE_IN_TB = 1099511627776;
private const int START_INDEX = 0;
private const string STRING_VALUE_ZERO = "0";
private const string STRING_VALUE_ONE = "1";
private const string SYMBOL_BYTES = "bytes";
private const string STRING_FORMAT = "n";
public const string FILESIZE_KB = "KB";
private const int FILE_SIZE_LIMIT = 100;
private const string STRING_FORMAT_UPTO_TWO_DECIMAL = ".00";
private const int FILE_SIZE_IN_BYTES = 102400;
private const int FILE_SIZE_IN_MegaBytes = 104857600;
private const long FILE_SIZE_IN_GIGABYTES = 107374182400;
private const string SYMBOL_SPACE = " ";
string size = FormatSize(fileSize,sizeOFFile);
private string FormatSize(Int64 fileSize, float sizeOFFile)
{
//Format number to KB
string stringSize = string.Empty;
NumberFormatInfo numberFormatInfo = new NumberFormatInfo();
Int64 fileSizeInKB = 0;
Int64 fileSizeInMB = 0;
long fileSizeInGB = 0;
float fileSizeInKBInFloat = 0;
float fileSizeInMBInFloat = 0;
float fileSizeInGBInFloat = 0;
string byteSize = string.Empty;
if (fileSize < FILESIZE_IN_KB)
{
if (fileSize == 0)
{
//zero byte
stringSize = STRING_VALUE_ZERO + SYMBOL_SPACE + SYMBOL_BYTES;
}
else
{
//less than 1K but not zero byte
stringSize = fileSize + SYMBOL_SPACE + SYMBOL_BYTES + SYMBOL_SPACE + SYMBOL_OPENING_BRACE + fileSize + SYMBOL_SPACE + SYMBOL_BYTES + SYMBOL_CLOSING_BRACE;
}
}
else if(fileSize < FILESIZE_IN_MB)
{
if (fileSize < FILE_SIZE_IN_BYTES)
{
fileSizeInKBInFloat = (sizeOFFile / (float)FILESIZE_IN_KB);
}
else
{
//convert to KB
fileSizeInKB = (fileSize / FILESIZE_IN_KB);
}
if (fileSizeInKBInFloat != 0)
{
stringSize = fileSizeInKBInFloat.ToString(STRING_FORMAT, numberFormatInfo);
}
else
{
//format number with default format
stringSize = fileSizeInKB.ToString(STRING_FORMAT, numberFormatInfo);
///If file Size is morethan 100 than replace decimal string to emptyString.
if (fileSizeInKB >= FILE_SIZE_LIMIT)
{
stringSize = stringSize.Replace(STRING_FORMAT_UPTO_TWO_DECIMAL, string.Empty);
}
}
///Sets the byteSize like 12345 into 12,345.
byteSize = fileSize.ToString(STRING_FORMAT, numberFormatInfo);
byteSize = byteSize.Replace(STRING_DECIMAL, string.Empty);
stringSize = stringSize + "KB" + SYMBOL_SPACE + SYMBOL_OPENING_BRACE + byteSize + SYMBOL_SPACE + SYMBOL_BYTES + SYMBOL_CLOSING_BRACE;
}
else if (fileSize < Constants.FILESIZE_IN_GB)
{
if (fileSize < FILE_SIZE_IN_MegaBytes)
{
fileSizeInMBInFloat = (sizeOFFile / (float)FILESIZE_IN_MB);
}
else
{
//convert to KB
fileSizeInMB = (fileSize / FILESIZE_IN_MB);
}
if (fileSizeInMBInFloat != 0)
{
stringSize = fileSizeInMBInFloat.ToString(STRING_FORMAT, numberFormatInfo);
}
else
{
//format number with default format
stringSize = fileSizeInMB.ToString(STRING_FORMAT, numberFormatInfo);
if (fileSizeInMB >= FILE_SIZE_LIMIT)
{
stringSize = stringSize.Replace(STRING_FORMAT_UPTO_TWO_DECIMAL, string.Empty);
}
}
///Sets the byteSize like 12345 into 12,345.
byteSize = fileSize.ToString(STRING_FORMAT, numberFormatInfo);
byteSize = byteSize.Replace(STRING_FORMAT_UPTO_TWO_DECIMAL, string.Empty);
stringSize = stringSize + "MB" + SYMBOL_SPACE + SYMBOL_OPENING_BRACE + byteSize +SYMBOL_SPACE + SYMBOL_BYTES + SYMBOL_CLOSING_BRACE;
}
else if(fileSize < FILESIZE_IN_TB)
{
if (fileSize < FILE_SIZE_IN_GIGABYTES)
{
fileSizeInGBInFloat = (sizeOFFile / (float)FILESIZE_IN_GB);
}
else
{
//convert to KB
fileSizeInGB = (fileSize / FILESIZE_IN_GB);
}
if (fileSizeInGBInFloat != 0)
{
stringSize = fileSizeInGBInFloat.ToString(STRING_FORMAT, numberFormatInfo);
}
else
{
//format number with default format
stringSize = fileSizeInGB.ToString(STRING_FORMAT, numberFormatInfo);
if (fileSizeInGB >= FILE_SIZE_LIMIT)
{
stringSize = stringSize.Replace(STRING_FORMAT_UPTO_TWO_DECIMAL, string.Empty);
}
}
///Sets the byteSize like 12345 into 12,345.
byteSize = fileSize.ToString(STRING_FORMAT, numberFormatInfo);
byteSize = byteSize.Replace(STRING_FORMAT_UPTO_TWO_DECIMAL, string.Empty);
stringSize = stringSize + "GB" + SYMBOL_SPACE + SYMBOL_OPENING_BRACE + byteSize + SYMBOL_SPACE + SYMBOL_BYTES + SYMBOL_CLOSING_BRACE;
}
return stringSize;
}