I have a C# function that reads the mapped drives on the system and displays a list of the network drives. For this I am using ManagementObjectSearcher
a query that returns the mapped logical drives.
I've been looking at the different properties and I can't find any that indicate if that unit is persistent or not (something that seems to be available if it is an online unit). Is there a property to see the persistence of the mapped drive? If not, how could it be checked?
The code I have looks like this (simplified):
using System;
using System.Collections.Generic;
using System.Management;
class MappedDrive
{
public char Letra { get; }
public string Ruta { get; }
public bool Persistente { get; }
public MappedDrive(char Letra, string Ruta, bool Persistente)
{
this.Letra = Letra;
this.Ruta = Ruta;
this.Persistente = Persistente;
}
// devuelve una lista de las unidades mapeadas en el sistema
public static List<MappedDrive> getUnidadesMapeadas()
{
List<MappedDrive> unidades = new List<MappedDrive>();
ManagementObjectSearcher buscador = new ManagementObjectSearcher("select * from Win32_MappedLogicalDisk");
foreach (ManagementObject unidad in buscador.Get())
{
unidades.Add(
new MappedDrive(
unidad["Name"].ToString()[0],
unidad["ProviderName"].ToString(),
true // <--- Esto es lo que no sé cómo leer
)
);
}
return unidades;
}
static void Main(string[] args)
{
List<MappedDrive> du = getUnidadesMapeadas();
foreach (MappedDrive unidad in du)
{
Console.WriteLine("{0}: mapeada a {1} (persistente: {2})", unidad.Letra, unidad.Ruta, unidad.Persistente);
}
}
}
Win32_MappedLogicalDisk
it certainly has no property that indicates whether a mapped drive is persistent. I guess you'll have to get the ip of the remote unit and look it up inWin32_NetworkConnection
. Something similar to this code, it is not very tested and may depend on the units, try it and let me know if it helps you.Edit
If it doesn't work with the properties I indicate (which work on network drives), you can try changing "Name" to "LocalName", and for the "ProviderName" path to "RemotePath".