DriveInfo[] allDrives = DriveInfo.GetDrives(); foreach (DriveInfo drive in allDrives) { Console.WriteLine("Drive {0}", drive.Name); Console.WriteLine(" File type: {0}", drive.DriveType); if (drive.IsReady == true) { Console.WriteLine(" Volume label: {0}", drive.VolumeLabel); Console.WriteLine(" File system: {0}", drive.DriveFormat); Console.WriteLine(" Available space to current user:{0, 15} bytes", drive.AvailableFreeSpace); Console.WriteLine(" Total available space: {0, 15} bytes", drive.TotalFreeSpace); Console.WriteLine(" Total size of drive: {0, 15} bytes", drive.TotalSize); } Console.WriteLine(); }
DriveInfo drive = new DriveInfo("C:"); if (drive.IsReady && drive.DriveType == DriveType.Fixed && drive.RootDirectory.Exists && drive.VolumeLabel != "") { bool isWritable = false; try { using (FileStream fs = new FileStream(Path.Combine(drive.RootDirectory.FullName, "test.txt"), FileMode.CreateNew, FileAccess.Write, FileShare.ReadWrite)) { using (StreamWriter sw = new StreamWriter(fs)) { sw.WriteLine("This is a test"); } } isWritable = true; File.Delete(Path.Combine(drive.RootDirectory.FullName, "test.txt")); } catch (Exception ex) { Console.WriteLine("An error occurred while trying to write to the drive: " + ex.Message); } if (isWritable) { Console.WriteLine("The drive is writable!"); } else { Console.WriteLine("The drive is not writable."); } }This code checks if the C:\ drive is writable by trying to create a test file on the root directory of the drive. It also checks if the drive is a fixed drive and has a volume label. Depending on the result, it prints out a message saying whether the drive is writable or not. Package Library: This package is part of the .NET Framework and doesn't require any additional package library to use.