Get File Bytes And Readable Size Information
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
namespace Candy.Client.Common
{
public static class FileHelper
{
public static byte[] GetFileBytes(FileInfo fInfo)
{
byte[] fileContent;
using (Stream fileStream = fInfo.OpenRead())
{
BinaryReader bin = new BinaryReader(fileStream);
fileContent = bin.ReadBytes((int)fInfo.Length);
bin.Dispose();
}
return fileContent;
}
public static string GetFileSizeLabel(FileInfo fInfo)
{
double fsize = fInfo.Length;
string sizeLabel = "Byte";
if (fsize > 1024)
{
fsize = fsize / 1024;
sizeLabel = "kb";
}
if (fsize > 1024)
{
fsize = fsize / 1024;
sizeLabel = "MB";
}
if (fsize > 1024)
{
fsize = fsize / 1024;
sizeLabel = "GB";
}
fsize = Math.Round(fsize, 2);
return fInfo.Name + " " + fsize + sizeLabel;
}
}
}
Related examples in the same category