Retrouver le chemin UNC d'un lecteur réseau.

Contenu du snippet

[DllImport("shlwapi.dll", SetLastError = true, CharSet = CharSet.Auto)] 
public static extern bool PathIsNetworkPath([MarshalAs(UnmanagedType.LPTStr)]string pszPath);
[DllImport("mpr.dll", SetLastError = true, CharSet = CharSet.Auto, EntryPoint = "WNetGetConnectionW")] 
public static extern int WNetGetConnection([MarshalAs(UnmanagedType.LPWStr)]string lpLocalName, StringBuilder lpRemoteName, ref int lpnLength);

public string GetUNCPath(string NetworkPath) 
{

  string szRoot = NetworkPath.Substring(0, 2);
  string szPath = NetworkPath;
  if (PathIsNetworkPath(szPath)) 
  {

     StringBuilder szbUNC = new StringBuilder(260);
     int iLength = 260;
     if (WNetGetConnection(szRoot, szbUNC, ref iLength) == ERROR_SUCCESS)
    {
       szPath = Path.Combine(szbUNC.ToString(), Path.GetFileName(szPath)); 
    }

  }
   return szPath;
}






Compatibilité : C# 1.x, C# 2.x

Disponible dans d'autres langages :

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.