Is Drive Ready

   
  

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

class Program {
    static void Main(string[] args) {
        // Get info regarding all drives.
        DriveInfo[] myDrives = DriveInfo.GetDrives();

        // Now print stats. 
        foreach (DriveInfo d in myDrives) {
            Console.WriteLine("Name: {0}", d.Name);
            Console.WriteLine("Type: {0}", d.DriveType);

            if (d.IsReady) {
                Console.WriteLine("Free space: {0}", d.TotalFreeSpace);
                Console.WriteLine("Format: {0}", d.DriveFormat);
                Console.WriteLine("Label: {0}
", d.VolumeLabel);
            }
        }
        Console.ReadLine();
    }
}

   
     


Removes a directory as best as it can. Errors are ignored.

   
 

//Microsoft Public License (Ms-PL)
//http://visualizer.codeplex.com/license

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;

namespace Redwerb.BizArk.Core.Util
{

    /// <summary>
    /// Provides methods that are useful when working with files and directories.
    /// </summary>
    public static class FileUtil
    {
        /// <summary>
        /// Removes a directory as best as it can. Errors are ignored.
        /// </summary>
        /// <param name="dirPath"></param>
        public static void RemoveDirectory(string dirPath)
        {
            foreach (string childDirPath in Directory.GetDirectories(dirPath))
                RemoveDirectory(childDirPath);

            foreach (string filePath in Directory.GetFiles(dirPath))
            {
                try
                {
                    File.Delete(filePath);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("Unable to delete " + filePath + ": " + ex.Message);
                }
            }

            try
            {
                Directory.Delete(dirPath);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Unable to delete " + dirPath + ": " + ex.Message);
            }
        }
    }
}

   
     


Get an array of files info from a directory.

#region License and Copyright
/* ————————————————————————-
* Dotnet Commons IO
*
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the
*
* Free Software Foundation, Inc.,
* 59 Temple Place,
* Suite 330,
* Boston,
* MA 02111-1307
* USA
*
* ————————————————————————-
*/
#endregion

using System;
using System.Collections;
using System.Globalization;
using System.IO;

namespace Dotnet.Commons.IO
{

///
///

/// This class provides basic facilities for manipulating files and file paths.
///
///

File-related methods

/// There are methods to
/// /// copy a file to another file,
/// compare the content of 2 files,
/// delete files using the wildcard character,
/// etc
///
///

///
public sealed class FileUtils
{
/// —————————————————————
///

/// Get an array of files info from a directory.
///

/// ///
/// —————————————————————
public static FileInfo[] GetFilesInDirectory(string directory)
{
if ((directory == null) || (directory.Length < 1)) throw new System.ArgumentException("Directory supplied is either null or empty"); DirectoryInfo dirInfo = new DirectoryInfo(directory); if (!dirInfo.Exists) throw new System.ArgumentException("Directory '" + directory + "' does not exist."); return dirInfo.GetFiles(); } } } [/csharp]

Determine if the directory is empty, ie. no files and no sub-directories

   
 

#region License and Copyright
/* -------------------------------------------------------------------------
 * Dotnet Commons IO
 *
 *
 * This library is free software; you can redistribute it and/or modify it 
 * under the terms of the GNU Lesser General Public License as published by 
 * the Free Software Foundation; either version 2.1 of the License, or 
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 
 * for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public License 
 * along with this library; if not, write to the 
 * 
 * Free Software Foundation, Inc., 
 * 59 Temple Place, 
 * Suite 330, 
 * Boston, 
 * MA 02111-1307 
 * USA 
 * 
 * -------------------------------------------------------------------------
 */
#endregion

using System;
using System.Collections;
using System.Globalization;
using System.IO;

namespace Dotnet.Commons.IO
{
  
  ///  
  /// <summary>  
  /// This class provides basic facilities for manipulating files and file paths.
    /// 
    /// <h3>File-related methods</h3>
    /// There are methods to 
    /// <list type="bullet">
    ///     <item>copy a file to another file,</item>
    ///     <item>compare the content of 2 files,</item>
    ///     <item>delete files using the wildcard character,</item>
    ///     <item>etc</item>
    /// </list>
  /// </summary>
  ///     
  public sealed class FileUtils
  {
        /// <summary>
        /// Determine if the directory is empty, ie. no files and no sub-directories
        /// </summary>
        /// <param name="path">directory to inspect</param>
        /// <returns>true if directory is empty, false otherwise</returns>
        public static bool IsDirectoryEmpty(string path)
        {
            return IsDirectoryEmpty(new DirectoryInfo(path));
        }

        /// <summary>
        /// Determine if the directory is empty, ie. no files and no sub-directories
        /// </summary>
        /// <param name="directory">directory to inspect</param>
        /// <returns>true if directory is empty, false otherwise</returns>
        public static bool IsDirectoryEmpty(DirectoryInfo directory)
        {
            FileInfo[] files = directory.GetFiles();
            DirectoryInfo[] subdirs = directory.GetDirectories();

            return (files.Length == 0 &amp;&amp; subdirs.Length == 0);
        }
   }
}

   
     


Gets the size of all files within a directory

   
 
/*
Copyright (c) 2010 <a href="http://www.gutgames.com">James Craig</a>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.*/

#region Usings
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;

#endregion

namespace Utilities
{
    public static class FileManager
    {
        /// <summary>
        /// Determines if a directory exists
        /// </summary>
        /// <param name="DirectoryPath">Path of the directory</param>
        /// <returns>true if it exists, false otherwise</returns>
        public static bool DirectoryExists(string DirectoryPath)
        {
            try
            {
                return Directory.Exists(DirectoryPath);
            }
            catch (Exception a)
            {
                throw a;
            }
        }
        /// <summary>
        /// Gets a list of files
        /// </summary>
        /// <param name="DirectoryPath">Directory to check for files</param>
        /// <param name="Recursive">Determines if this is a recursive look at all directories under this one</param>
        /// <returns>a list of files</returns>
        public static List<FileInfo> FileList(string DirectoryPath,bool Recursive)
        {
            try
            {
                if (!Recursive)
                {
                    return FileList(DirectoryPath);
                }
                List<FileInfo> Files = new List<FileInfo>();
                if (DirectoryExists(DirectoryPath))
                {
                    DirectoryInfo Directory = new DirectoryInfo(DirectoryPath);
                    FileInfo[] SubFiles = Directory.GetFiles();
                    foreach (FileInfo SubFile in SubFiles)
                    {
                        Files.Add(SubFile);
                    }
                    DirectoryInfo[] SubDirectories = Directory.GetDirectories();
                    foreach (DirectoryInfo SubDirectory in SubDirectories)
                    {
                        List<FileInfo> TempFiles = FileList(SubDirectory.FullName, Recursive);
                        foreach (FileInfo File in TempFiles)
                        {
                            Files.Add(File);
                        }
                    }
                }
                return Files;
            }
            catch (Exception a)
            {
                throw a;
            }
        }        
        /// <summary>
        /// Gets a list of files
        /// </summary>
        /// <param name="DirectoryPath">Directory to check for files</param>
        /// <returns>a list of files</returns>
        public static List<FileInfo> FileList(string DirectoryPath)
        {
            try
            {
                List<FileInfo> Files = new List<FileInfo>();
                if (DirectoryExists(DirectoryPath))
                {
                    DirectoryInfo Directory = new DirectoryInfo(DirectoryPath);
                    FileInfo[] SubFiles = Directory.GetFiles();
                    foreach (FileInfo SubFile in SubFiles)
                    {
                        Files.Add(SubFile);
                    }
                }
                return Files;
            }
            catch (Exception a)
            {
                throw a;
            }
        }
        /// <summary>
        /// Gets the size of all files within a directory
        /// </summary>
        /// <param name="Directory">Directory path</param>
        /// <param name="Recursive">determines if this is a recursive call or not</param>
        /// <returns></returns>
        public static long GetDirectorySize(string Directory, bool Recursive)
        {
            try
            {
                long Size = 0;
                List<FileInfo> Files = FileManager.FileList(Directory, Recursive);
                foreach (FileInfo File in Files)
                {
                    Size += File.Length;
                }
                return Size;
            }
            catch (Exception a)
            {
                throw a;
            }
        }
    }
}

   
     


Directory Tree Host


   
 
/*
User Interfaces in C#: Windows Forms and Custom Controls
by Matthew MacDonald

Publisher: Apress
ISBN: 1590590457
*/

using System.IO;

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace DirectoryTreeHost
{
    /// <summary>
    /// Summary description for DirectoryTreeHost.
    /// </summary>
    public class DirectoryTreeHost : System.Windows.Forms.Form
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public DirectoryTreeHost()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if(components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            // 
            // DirectoryTreeHost
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(292, 266);
            this.Name = "DirectoryTreeHost";
            this.Text = "DirectoryTreeHost";
            this.Load += new System.EventHandler(this.DirectoryTreeHost_Load);

        }
        #endregion

        private void DirectoryTreeHost_Load(object sender, System.EventArgs e)
        {
            DirectoryTree dirTree = new 
                DirectoryTree();
            dirTree.Size = new Size(this.Width - 30, this.Height - 60);
            dirTree.Location = new Point(5, 5);
            dirTree.Drive = Char.Parse("C");
            this.Controls.Add(dirTree);

        }

        public static void Main()
        {
            Application.Run(new DirectoryTreeHost());
        }
    }
    public class DirectoryTree : TreeView
    {
        public delegate void DirectorySelectedDelegate(object sender,
            DirectorySelectedEventArgs e);
        public event DirectorySelectedDelegate DirectorySelected;
        
        private Char drive;
        public Char Drive
        {
            get
            {
                return drive;
            }
            set
            {
                drive = value;
                RefreshDisplay();
            }
        }
        
        // This is public so a Refresh can be triggered manually.
        public void RefreshDisplay()
        {
            // Erase the existing tree.
            this.Nodes.Clear();
            
            // Set the first node.
            TreeNode rootNode = new TreeNode(drive + ":");
            this.Nodes.Add(rootNode);
            
            // Fill the first level and expand it.
            Fill(rootNode);
            this.Nodes[0].Expand();
        }
        
        private void Fill(TreeNode dirNode)
        {
            DirectoryInfo dir = new DirectoryInfo(dirNode.FullPath);
            
            // An exception could be thrown in this code if you don&#039;t
            // have sufficient security permissions for a file or directory.
            // You can catch and then ignore this exception.
            
            foreach (DirectoryInfo dirItem in dir.GetDirectories())
            {
                // Add node for the directory.
                TreeNode newNode = new TreeNode(dirItem.Name);
                dirNode.Nodes.Add(newNode);
                newNode.Nodes.Add("*");
            }
        }
        
        protected override void OnBeforeExpand(TreeViewCancelEventArgs e)
        {
            base.OnBeforeExpand(e);

            // If a dummy node is found, remove it and read the real directory list.
            if (e.Node.Nodes[0].Text == "*")
            {
                e.Node.Nodes.Clear();
                Fill(e.Node);
            }
        }
        
        protected override void OnAfterSelect(TreeViewEventArgs e)
        {
            base.OnAfterSelect(e);
            
            // Raise the DirectorySelected event.
            if (DirectorySelected != null)
            {
                DirectorySelected(this,
                    new DirectorySelectedEventArgs(e.Node.FullPath));
            }
        }
    }
    public class DirectorySelectedEventArgs : EventArgs
    {
        public string DirectoryName;

        public DirectorySelectedEventArgs(string directoryName)
        {
            this.DirectoryName = directoryName;
        }
    }



}