Encoding in action


   

using System;
using System.IO;
using System.Text;

class Test
{
    public static void Main() 
    {        
        using (StreamWriter output = new StreamWriter("practice.txt")) 
        {
            // Create and write a string containing the symbol for Pi.
            string srcString = "Area = u03A0r^2";

            // Convert the UTF-16 encoded source string to UTF-8 and ASCII.
            byte[] utf8String = Encoding.UTF8.GetBytes(srcString);
            byte[] asciiString = Encoding.ASCII.GetBytes(srcString);
            
            // Write the UTF-8 and ASCII encoded byte arrays. 
            output.WriteLine("UTF-8  Bytes: {0}", BitConverter.ToString(utf8String));
            output.WriteLine("ASCII  Bytes: {0}", BitConverter.ToString(asciiString));
            
            
            // Convert UTF-8 and ASCII encoded bytes back to UTF-16 encoded  
            // string and write.
            output.WriteLine("UTF-8  Text : {0}", Encoding.UTF8.GetString(utf8String));
            output.WriteLine("ASCII  Text : {0}", Encoding.ASCII.GetString(asciiString));

            Console.WriteLine(Encoding.UTF8.GetString(utf8String));
            Console.WriteLine(Encoding.ASCII.GetString(asciiString));
        }
    }
}




           
          


Convert Byte array to Base64 Char Array


   


using System;
using System.IO;

class Test {
    public static void Main() {
        // The size of the char[] must 
        // be at least 4/3 the size of the source byte[] and must be 
        // divisible by 4.
        byte[] data = { 0x01, 0x02, 0x5A, 0xFF, 0x0, 0xF0, 0x4D, 0x62, 0x78,  
            0xD2, 0xC5, 0xA1, 0x5A, 0xD6, 0x0C, 0xA9, 0xA6, 0x63, 0x3D, 0xC2, 
            0xD5, 0x0F, 0xCC, 0x01, 0xF2, 0x0C};

        char[] base64data =  new char[(int)(Math.Ceiling((double)data.Length / 3) * 4)];

        Console.WriteLine("
Byte array encoding/decoding");
        Convert.ToBase64CharArray(data, 0, data.Length, base64data, 0);
        Console.WriteLine(new String(base64data));
    }
}

           
          


Check drive free space.

   
 

//http://bbinjest.codeplex.com/
//Apache License 2.0 (Apache)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Globalization;

namespace BneyBaruch.Ingest.Utils.FileSystem
{
    public static class DriveUtil
    {


        /// <summary>
        ///     Check drive free space.
        /// </summary>
        /// <param name="driveName">Drive name to check free space.</param>
        /// <param name="minimumSpace">Minimum space in bytes.</param>
        /// <returns></returns>
        public static bool IsEnoughSpace(string driveName, long minimumSpace)
        {
            if (string.IsNullOrEmpty(driveName))
                throw new ArgumentNullException("driveName");

            string formattedDriveName = driveName + @":";

            DriveInfo[] drives = DriveInfo.GetDrives();

            DriveInfo drive = drives.FirstOrDefault(
                delegate(DriveInfo innerDrive)
                {
                    return string.Compare(innerDrive.Name, formattedDriveName, true) == 0;
                }
            );

            if (drive == null)
                throw new DriveNotFoundException(string.Format("Drive {0} could not be found.", formattedDriveName));

            if( !drive.IsReady)
                throw new DriveNotFoundException(string.Format("Drive {0} is not ready.", formattedDriveName));

            return drive.TotalFreeSpace > minimumSpace;
        }
   }
}

   
     


Root Directory

   
  

using System;
using System.IO;

class MainClass {
    static void Main(string[] args) {
        if (args.Length == 1) {
            DriveInfo drive = new DriveInfo(args[0]);

            Console.Write("Free space in {0}-drive (in kilobytes): ", args[0]);
            Console.WriteLine(drive.AvailableFreeSpace / 1024);
            return;
        }

        foreach (DriveInfo drive in DriveInfo.GetDrives()) {
            Console.WriteLine("{0} - {1} KB",drive.RootDirectory,
                    drive.AvailableFreeSpace / 1024
                    );
        }
    }
}

   
     


Drive Type

   
  

using System;
using System.IO;

class MainClass {
    static void Main(string[] args) {
        FileInfo file = new FileInfo("c:a.txt");

        // Display drive information.
        DriveInfo drv = new DriveInfo(file.FullName);

        Console.Write("Drive: ");
        Console.WriteLine(drv.Name);

        if (drv.IsReady) {
            Console.Write("Drive type: ");
            Console.WriteLine(drv.DriveType.ToString());
        }
    }
}

   
     


Available Free Space

   
  

using System;
using System.IO;

class MainClass {
    static void Main(string[] args) {
        FileInfo file = new FileInfo("c:a.txt");

        // Display drive information.
        DriveInfo drv = new DriveInfo(file.FullName);

        Console.Write("Drive: ");
        Console.WriteLine(drv.Name);

        if (drv.IsReady) {
            Console.Write("Drive free space: ");
            Console.WriteLine(drv.AvailableFreeSpace.ToString());
        }
    }
}

   
     


Drive Format

   
  
using System;
using System.IO;

class MainClass {
    static void Main(string[] args) {
        FileInfo file = new FileInfo("c:a.txt");
        DriveInfo drv = new DriveInfo(file.FullName);

        Console.Write("Drive: ");
        Console.WriteLine(drv.Name);

        if (drv.IsReady) {
            Console.WriteLine(drv.DriveFormat.ToString());
        }
    }
}