DriveInfo DriveInfo DriveInfo DriveInfo Class

定義

提供對磁碟上資訊的存取。Provides access to information on a drive.

public ref class DriveInfo sealed : System::Runtime::Serialization::ISerializable
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public sealed class DriveInfo : System.Runtime.Serialization.ISerializable
type DriveInfo = class
    interface ISerializable
Public NotInheritable Class DriveInfo
Implements ISerializable
繼承
DriveInfoDriveInfoDriveInfoDriveInfo
屬性
實作

範例

下列程式碼範例示範使用DriveInfo類別,以顯示目前的系統上的所有磁碟機的相關資訊。The following code example demonstrates the use of the DriveInfo class to display information about all of the drives on the current system.

using System;
using System.IO;

class Test
{
    public static void Main()
    {
        DriveInfo[] allDrives = DriveInfo.GetDrives();

        foreach (DriveInfo d in allDrives)
        {
            Console.WriteLine("Drive {0}", d.Name);
            Console.WriteLine("  Drive type: {0}", d.DriveType);
            if (d.IsReady == true)
            {
                Console.WriteLine("  Volume label: {0}", d.VolumeLabel);
                Console.WriteLine("  File system: {0}", d.DriveFormat);
                Console.WriteLine(
                    "  Available space to current user:{0, 15} bytes", 
                    d.AvailableFreeSpace);

                Console.WriteLine(
                    "  Total available space:          {0, 15} bytes",
                    d.TotalFreeSpace);

                Console.WriteLine(
                    "  Total size of drive:            {0, 15} bytes ",
                    d.TotalSize);
            }
        }
    }
}
/* 
This code produces output similar to the following:

Drive A:\
  Drive type: Removable
Drive C:\
  Drive type: Fixed
  Volume label: 
  File system: FAT32
  Available space to current user:     4770430976 bytes
  Total available space:               4770430976 bytes
  Total size of drive:                10731683840 bytes 
Drive D:\
  Drive type: Fixed
  Volume label: 
  File system: NTFS
  Available space to current user:    15114977280 bytes
  Total available space:              15114977280 bytes
  Total size of drive:                25958948864 bytes 
Drive E:\
  Drive type: CDRom

The actual output of this code will vary based on machine and the permissions
granted to the user executing it.
*/
Imports System
Imports System.IO

Class Test
    Public Shared Sub Main()
        Dim allDrives() As DriveInfo = DriveInfo.GetDrives()

        Dim d As DriveInfo
        For Each d In allDrives
            Console.WriteLine("Drive {0}", d.Name)
            Console.WriteLine("  Drive type: {0}", d.DriveType)
            If d.IsReady = True Then
                Console.WriteLine("  Volume label: {0}", d.VolumeLabel)
                Console.WriteLine("  File system: {0}", d.DriveFormat)
                Console.WriteLine( _
                    "  Available space to current user:{0, 15} bytes", _
                    d.AvailableFreeSpace)

                Console.WriteLine( _
                    "  Total available space:          {0, 15} bytes", _
                    d.TotalFreeSpace)

                Console.WriteLine( _
                    "  Total size of drive:            {0, 15} bytes ", _
                    d.TotalSize)
            End If
        Next
    End Sub
End Class
'This code produces output similar to the following:
'
'Drive A:\
'  Drive type: Removable
'Drive C:\
'  Drive type: Fixed
'  Volume label: 
'  File system: FAT32
'  Available space to current user:     4770430976 bytes
'  Total available space:               4770430976 bytes
'  Total size of drive:                10731683840 bytes 
'Drive D:\
'  Drive type: Fixed
'  Volume label: 
'  File system: NTFS
'  Available space to current user:    15114977280 bytes
'  Total available space:              15114977280 bytes
'  Total size of drive:                25958948864 bytes 
'Drive E:\
'  Drive type: CDRom
'
'The actual output of this code will vary based on machine and the permissions
'granted to the user executing it.

備註

這個類別建立模型的磁碟機,並提供方法和屬性來查詢磁碟機資訊。This class models a drive and provides methods and properties to query for drive information. 使用DriveInfo來判斷哪個磁碟機可供使用,以及它們是何種類型的磁碟機。Use DriveInfo to determine what drives are available, and what type of drives they are. 您也可以查詢來決定容量和磁碟機上的可用空間。You can also query to determine the capacity and available free space on the drive.

建構函式

DriveInfo(String) DriveInfo(String) DriveInfo(String) DriveInfo(String)

提供對指定磁碟上資訊的存取。Provides access to information on the specified drive.

屬性

AvailableFreeSpace AvailableFreeSpace AvailableFreeSpace AvailableFreeSpace

表示磁碟機上的可用空間量,以位元組為單位。Indicates the amount of available free space on a drive, in bytes.

DriveFormat DriveFormat DriveFormat DriveFormat

取得檔案系統的名稱,例如 NTFS 或 FAT32。Gets the name of the file system, such as NTFS or FAT32.

DriveType DriveType DriveType DriveType

取得磁碟類型,例如 CD-ROM、卸除式、網路或固定式。Gets the drive type, such as CD-ROM, removable, network, or fixed.

IsReady IsReady IsReady IsReady

取得值,這個值表示磁碟機是否已就緒。Gets a value that indicates whether a drive is ready.

Name Name Name Name

取得磁碟機名稱,例如 C:\。Gets the name of a drive, such as C:\.

RootDirectory RootDirectory RootDirectory RootDirectory

取得磁碟的根目錄。Gets the root directory of a drive.

TotalFreeSpace TotalFreeSpace TotalFreeSpace TotalFreeSpace

取得磁碟機上可用空間的總量,以位元組為單位。Gets the total amount of free space available on a drive, in bytes.

TotalSize TotalSize TotalSize TotalSize

取得磁碟機上儲存空間的總大小,以位元組為單位。Gets the total size of storage space on a drive, in bytes.

VolumeLabel VolumeLabel VolumeLabel VolumeLabel

取得或設定磁碟的磁碟區標籤。Gets or sets the volume label of a drive.

方法

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetDrives() GetDrives() GetDrives() GetDrives()

擷取電腦上所有邏輯磁碟的磁碟名稱。Retrieves the drive names of all logical drives on a computer.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

將磁碟名稱以字串方式傳回。Returns a drive name as a string.

明確介面實作

ISerializable.GetObjectData(SerializationInfo, StreamingContext) ISerializable.GetObjectData(SerializationInfo, StreamingContext) ISerializable.GetObjectData(SerializationInfo, StreamingContext) ISerializable.GetObjectData(SerializationInfo, StreamingContext)

將序列化目標物件所需要的資料填入 SerializationInfo 物件中。Populates a SerializationInfo object with the data needed to serialize the target object.

安全性

FileIOPermission
用來存取目錄資訊。for accessing directory information. 類別建構函式需要此權限。The class constructor requires this permission. 關聯的列舉型別:PathDiscoveryAssociated enumeration: PathDiscovery.

適用於