mscorlib(4.0.0.0) API with additions
DriveInfo.cs
1 using Microsoft.Win32;
4 using System.Security;
6 using System.Text;
7 
8 namespace System.IO
9 {
11  [Serializable]
12  [ComVisible(true)]
13  public sealed class DriveInfo : ISerializable
14  {
15  private string _name;
16 
17  private const string NameField = "_name";
18 
21  public string Name => _name;
22 
25  public DriveType DriveType
26  {
27  [SecuritySafeCritical]
28  get
29  {
30  return (DriveType)Win32Native.GetDriveType(Name);
31  }
32  }
33 
39  public string DriveFormat
40  {
41  [SecuritySafeCritical]
42  get
43  {
44  StringBuilder volumeName = new StringBuilder(50);
45  StringBuilder stringBuilder = new StringBuilder(50);
46  int errorMode = Win32Native.SetErrorMode(1);
47  try
48  {
49  if (!Win32Native.GetVolumeInformation(Name, volumeName, 50, out int _, out int _, out int _, stringBuilder, 50))
50  {
51  int lastWin32Error = Marshal.GetLastWin32Error();
52  __Error.WinIODriveError(Name, lastWin32Error);
53  }
54  }
55  finally
56  {
57  Win32Native.SetErrorMode(errorMode);
58  }
59  return stringBuilder.ToString();
60  }
61  }
62 
66  public bool IsReady
67  {
68  [SecuritySafeCritical]
69  get
70  {
71  return Directory.InternalExists(Name);
72  }
73  }
74 
79  public long AvailableFreeSpace
80  {
81  [SecuritySafeCritical]
82  get
83  {
84  int errorMode = Win32Native.SetErrorMode(1);
85  try
86  {
87  if (Win32Native.GetDiskFreeSpaceEx(Name, out long freeBytesForUser, out long _, out long _))
88  {
89  return freeBytesForUser;
90  }
91  __Error.WinIODriveError(Name);
92  return freeBytesForUser;
93  }
94  finally
95  {
96  Win32Native.SetErrorMode(errorMode);
97  }
98  }
99  }
100 
106  public long TotalFreeSpace
107  {
108  [SecuritySafeCritical]
109  get
110  {
111  int errorMode = Win32Native.SetErrorMode(1);
112  try
113  {
114  if (Win32Native.GetDiskFreeSpaceEx(Name, out long _, out long _, out long freeBytes))
115  {
116  return freeBytes;
117  }
118  __Error.WinIODriveError(Name);
119  return freeBytes;
120  }
121  finally
122  {
123  Win32Native.SetErrorMode(errorMode);
124  }
125  }
126  }
127 
133  public long TotalSize
134  {
135  [SecuritySafeCritical]
136  get
137  {
138  int errorMode = Win32Native.SetErrorMode(1);
139  try
140  {
141  if (Win32Native.GetDiskFreeSpaceEx(Name, out long _, out long totalBytes, out long _))
142  {
143  return totalBytes;
144  }
145  __Error.WinIODriveError(Name);
146  return totalBytes;
147  }
148  finally
149  {
150  Win32Native.SetErrorMode(errorMode);
151  }
152  }
153  }
154 
158 
165  public string VolumeLabel
166  {
167  [SecuritySafeCritical]
168  get
169  {
170  StringBuilder stringBuilder = new StringBuilder(50);
171  StringBuilder fileSystemName = new StringBuilder(50);
172  int errorMode = Win32Native.SetErrorMode(1);
173  try
174  {
175  if (!Win32Native.GetVolumeInformation(Name, stringBuilder, 50, out int _, out int _, out int _, fileSystemName, 50))
176  {
177  int num = Marshal.GetLastWin32Error();
178  if (num == 13)
179  {
180  num = 15;
181  }
182  __Error.WinIODriveError(Name, num);
183  }
184  }
185  finally
186  {
187  Win32Native.SetErrorMode(errorMode);
188  }
189  return stringBuilder.ToString();
190  }
191  [SecuritySafeCritical]
192  set
193  {
194  string path = _name + ".";
196  int errorMode = Win32Native.SetErrorMode(1);
197  try
198  {
199  if (!Win32Native.SetVolumeLabel(Name, value))
200  {
201  int lastWin32Error = Marshal.GetLastWin32Error();
202  if (lastWin32Error == 5)
203  {
204  throw new UnauthorizedAccessException(Environment.GetResourceString("InvalidOperation_SetVolumeLabelFailed"));
205  }
206  __Error.WinIODriveError(Name, lastWin32Error);
207  }
208  }
209  finally
210  {
211  Win32Native.SetErrorMode(errorMode);
212  }
213  }
214  }
215 
221  [SecuritySafeCritical]
222  public DriveInfo(string driveName)
223  {
224  if (driveName == null)
225  {
226  throw new ArgumentNullException("driveName");
227  }
228  if (driveName.Length == 1)
229  {
230  _name = driveName + ":\\";
231  }
232  else
233  {
234  Path.CheckInvalidPathChars(driveName);
235  _name = Path.GetPathRoot(driveName);
236  if (_name == null || _name.Length == 0 || _name.StartsWith("\\\\", StringComparison.Ordinal))
237  {
238  throw new ArgumentException(Environment.GetResourceString("Arg_MustBeDriveLetterOrRootDir"));
239  }
240  }
241  if (_name.Length == 2 && _name[1] == ':')
242  {
243  _name += "\\";
244  }
245  char c = driveName[0];
246  if ((c < 'A' || c > 'Z') && (c < 'a' || c > 'z'))
247  {
248  throw new ArgumentException(Environment.GetResourceString("Arg_MustBeDriveLetterOrRootDir"));
249  }
250  string path = _name + ".";
251  new FileIOPermission(FileIOPermissionAccess.PathDiscovery, path).Demand();
252  }
253 
254  [SecurityCritical]
255  private DriveInfo(SerializationInfo info, StreamingContext context)
256  {
257  _name = (string)info.GetValue("_name", typeof(string));
258  string path = _name + ".";
259  new FileIOPermission(FileIOPermissionAccess.PathDiscovery, path).Demand();
260  }
261 
266  public static DriveInfo[] GetDrives()
267  {
268  string[] logicalDrives = Directory.GetLogicalDrives();
269  DriveInfo[] array = new DriveInfo[logicalDrives.Length];
270  for (int i = 0; i < logicalDrives.Length; i++)
271  {
272  array[i] = new DriveInfo(logicalDrives[i]);
273  }
274  return array;
275  }
276 
279  public override string ToString()
280  {
281  return Name;
282  }
283 
287  [SecurityCritical]
289  {
290  info.AddValue("_name", _name, typeof(string));
291  }
292  }
293 }
Exposes instance methods for creating, moving, and enumerating through directories and subdirectories...
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
FileIOPermissionAccess
Specifies the type of file access requested.
unsafe override string ToString()
Converts the value of this instance to a T:System.String.
StringComparison
Specifies the culture, case, and sort rules to be used by certain overloads of the M:System....
override string ToString()
Returns a drive name as a string.
Definition: DriveInfo.cs:279
Definition: __Canon.cs:3
string VolumeLabel
Gets or sets the volume label of a drive.
Definition: DriveInfo.cs:166
static string GetPathRoot(string path)
Gets the root directory information of the specified path.
Definition: Path.cs:961
Describes the source and destination of a given serialized stream, and provides an additional caller-...
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
static DriveInfo [] GetDrives()
Retrieves the drive names of all logical drives on a computer.
Definition: DriveInfo.cs:266
DriveType
Defines constants for drive types, including CDRom, Fixed, Network, NoRootDirectory,...
Definition: DriveType.cs:8
long TotalSize
Gets the total size of storage space on a drive, in bytes.
Definition: DriveInfo.cs:134
Exposes static methods for creating, moving, and enumerating through directories and subdirectories....
Definition: Directory.cs:14
string Name
Gets the name of a drive, such as C:.
Definition: DriveInfo.cs:21
long TotalFreeSpace
Gets the total amount of free space available on a drive, in bytes.
Definition: DriveInfo.cs:107
Provides a collection of methods for allocating unmanaged memory, copying unmanaged memory blocks,...
Definition: Marshal.cs:15
Provides access to information on a drive.
Definition: DriveInfo.cs:13
bool IsReady
Gets a value that indicates whether a drive is ready.
Definition: DriveInfo.cs:67
Stores all the data needed to serialize or deserialize an object. This class cannot be inherited.
Represents a mutable string of characters. This class cannot be inherited.To browse the ....
long AvailableFreeSpace
Indicates the amount of available free space on a drive, in bytes.
Definition: DriveInfo.cs:80
The exception that is thrown when one of the arguments provided to a method is not valid.
void Demand()
Forces a T:System.Security.SecurityException at run time if all callers higher in the call stack have...
Allows an object to control its own serialization and deserialization.
Definition: ISerializable.cs:8
static string [] GetLogicalDrives()
Retrieves the names of the logical drives on this computer in the form "<drive letter>:\".
Definition: Directory.cs:1217
Specifies that the class can be serialized.
string DriveFormat
Gets the name of the file system, such as NTFS or FAT32.
Definition: DriveInfo.cs:40
DirectoryInfo RootDirectory
Gets the root directory of a drive.
Definition: DriveInfo.cs:157
The exception that is thrown when the operating system denies access because of an I/O error or a spe...
Controls the ability to access files and folders. This class cannot be inherited.
static int GetLastWin32Error()
Returns the error code returned by the last unmanaged function that was called using platform invoke ...
void GetObjectData(SerializationInfo info, StreamingContext context)
Populates a T:System.Runtime.Serialization.SerializationInfo with the data needed to serialize the ta...
Performs operations on T:System.String instances that contain file or directory path information....
Definition: Path.cs:13
DriveInfo(string driveName)
Provides access to information on the specified drive.
Definition: DriveInfo.cs:222