Quiero hacer un programa en consola C#, en el cual me muestre cuantas hay y su información. Solo quiero que me muestre unidades de discos DVD, aunque sean SATA, IDE o por USB.
Por ejemplo:
Unidad F:
Etiqueta de volumen : 58 Fotos 2020 Tamaño total de la unidad: 4,26 GB.
Unidad G:
Etiqueta de volumen : Visual Tamaño total de la unidad: 3,09 GB.
Quiero hacerlo así y ya está. El ejemplo que he visto te cuenta todas las unidades como indica abajo y no me interesa.
using System;
using System.IO;
namespace Informacion_lector_Consola_01
{
class Program
{
static void Main(string[] args)
{
#region Configuración ventana.
// Título de la ventana.
Console.Title = "Información lector.";
// Tamaño de la ventana, x, y.
Console.SetWindowSize(80, 35);
// Color de fondo.
Console.BackgroundColor = ConsoleColor.White;
// Color de las letras.
Console.ForegroundColor = ConsoleColor.Black;
// Limpiar pantalla y dejarlo todo en color de fondo.
Console.Clear();
// Visible el cursor.
Console.CursorVisible = true;
#endregion
DriveInfo[] allDrives = DriveInfo.GetDrives();
foreach (DriveInfo d in allDrives)
{
Console.WriteLine("Unidad {0}", d.Name);
Console.WriteLine(" Tipo de unidad: {0}", d.DriveType);
if (d.IsReady == true)
{
Console.WriteLine(" Etiqueta de volumen : {0}", d.VolumeLabel);
Console.WriteLine(" Sistema de archivo: {0}", d.DriveFormat);
Console.WriteLine(
" Espacio disponible para el usuario actual:{0, 15} bytes",
d.AvailableFreeSpace);
Console.WriteLine(
" Espacio total disponible: {0, 15} bytes",
d.TotalFreeSpace);
Console.WriteLine(
" Tamaño total de la unidad: {0, 15} bytes ",
d.TotalSize);
}
}
// Pulse cualquier tecla para continuar.
Console.ReadKey();
}
}
}
¿Alguna idea?
Saludos.
Te dejo dos opciones que te pueden servir. Ambas están sacadas del siguiente link: https://bytes.com/topic/c-sharp/answers/257251-how-detect-hard-drives
using System;
using System.Runtime.InteropServices;
public class ListDrives {
public enum DriveType : int {
Unknown = 0,
NoRoot = 1,
Removable = 2,
Localdisk = 3,
Network = 4,
CD = 5,
RAMDrive = 6
}
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern int GetDriveType(string s);
public static void Main(string[] args) {
foreach(string s in Environment.GetLogicalDrives())
Console
.WriteLine(string.Format("Drive {0} is a {1}", s,
Enum.GetName(typeof(DriveType
), GetDriveType
(s
)))); }
}
using System;
using System.Management;
public class ListDrives {
public static void Main(string[] args) {
string query = "SELECT * FROM win32_logicaldisk";
ManagementObjectSearcher searcher
= new ManagementObjectSearcher
(query
); foreach(ManagementObject obj in searcher.Get())
Console.WriteLine("Objeto: " + obj.GetText(TextFormat.Mof)); // Aqui tendrias que filtrar segun DriveType
}
}
Muchísimas gracias. Voy a experimentar.
;-) ;-) ;-)