Get File Bytes And Readable Size Information : File Command « File Stream « C# / C Sharp






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

1.C# Implementation of the File Copy Command
2.Use the FileInfo Class to Delete Files with Ease
3.File Move Implementation
4.Rename File Name
5.Rename File Extension
6.Backup File
7.Copy directory
8.Find string in a file
9.Converts a number of bytes to a shorter, more readable string representation
10.Compares the specified file1. This method accepts two strings the represent two files to compare.
11.File Renamer
12.Format File Size
13.Is File Existed
14.Is File readable or writeable
15.Replace in file
16.Send a file to the recycle bin
17.Copy file and folder
18.Gets last write time for a file.
19.Execute File
20.Gets the file size text.
21.Copy folder
22.Empty a Folder