c# – How do I know if a mapped drive is persistent?

Question:

I have a function in C # that reads the mapped drives on the system and displays a list of the network drives. For this I am using ManagementObjectSearcher with a query that returns me the mapped logical units.

I have 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 can it be verified?

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);
        }
    }
}

Answer:

Win32_MappedLogicalDisk certainly doesn't have any property that indicates whether a mapped drive is persistent. I guess you will have to get the ip of the remote unit and look for it in Win32_NetworkConnection . Something similar to this code, it is not very tested and may depend on the units, try it and tell me if it helps you.

ManagementObjectSearcher buscador = new ManagementObjectSearcher("select * from Win32_MappedLogicalDisk");


foreach (System.Management.ManagementObject unidad in buscador.Get())
{
     var path= unidad["ProviderName"].ToString().Split(new char[] { '\\' },StringSplitOptions.RemoveEmptyEntries);
     ManagementObjectSearcher buscador2 = new ManagementObjectSearcher("select * from Win32_NetworkConnection where RemoteName like '%" + path[0] + "%'");
     var persistent=buscador2.Get().OfType<System.Management.ManagementObject>().FirstOrDefault()?["persistent"];
     if (persistent!=null)
     {
          unidades.Add(
                new MappedDrive(
                    unidad["Name"].ToString()[0],
                    unidad["ProviderName"].ToString(),
                    (bool)persistent
                )
            );
     }
     else
     { 
          //no se encontró unidad de red
          unidades.Add(
                new MappedDrive(
                    unidad["Name"].ToString()[0],
                    unidad["ProviderName"].ToString(),
                    false
                )
            );         
     }
}

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 path "ProviderName" to "RemotePath".

Scroll to Top