Csharp/CSharp Tutorial/File Directory Stream/Drive — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 12:20, 26 мая 2010

DriveInfo App

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

  class Program
  {
    static void Main(string[] args)
    {
      DriveInfo[] myDrives = DriveInfo.GetDrives();
      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);
        }
      }
    }
  }

DriveInfo: name, type, format and available free space

using System;
using System.IO;
static class MainClass
{
    static void Main(string[] args)
    {
        FileInfo file = new FileInfo("c:\\test.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());
            Console.Write("Drive format: ");
            Console.WriteLine(drv.DriveFormat.ToString());
            Console.Write("Drive free space: ");
            Console.WriteLine(drv.AvailableFreeSpace.ToString());
        }
    }
}
Drive: c:\
Drive type: Fixed
Drive format: NTFS
Drive free space: 33105936384

Get all logical drives

using System;
using System.IO;
class MaionClass
{
  public static void Main(String[] args)
  {
    string[] drives = Directory.GetLogicalDrives();
    Console.WriteLine("Here are your drives:");
    foreach(string s in drives)
    {
      Console.WriteLine("--> {0}", s);
    }
  }
}
Here are your drives:
--> C:\
--> D:\

List all drives

using System;
using System.IO;
static class MainClass
{
    static void Main(string[] args)
    {
        foreach (DriveInfo drive in DriveInfo.GetDrives())
        {
            try
            {
                Console.WriteLine(
                    "{0} - {1} KB",
                    drive.RootDirectory,
                    drive.AvailableFreeSpace / 1024
                    );
            } catch (IOException) {
                Console.WriteLine(drive);
            }
        }
    }
}
C:\ - 32330000 KB
D:\