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());
}
}
}
Month: February 2011
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());
}
}
}
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();
}
}
DriveInfo(file.FullName) Name
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);
}
}
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
{
///
///
///
///
File-related methods
/// There are methods to
///
///
///
///
///
///
///
///
public sealed class FileUtils
{
/// —————————————————————
///
///
///
///
/// —————————————————————
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]

