Je pense que la réponse complète à cette question est d'inclure l'utilisation de la propriété DriveInfo.DriveType.
public static bool IsNetworkPath(string path)
{
if (!path.StartsWith(@"/") && !path.StartsWith(@"\"))
{
string rootPath = System.IO.Path.GetPathRoot(path); // get drive's letter
System.IO.DriveInfo driveInfo = new System.IO.DriveInfo(rootPath); // get info about the drive
return driveInfo.DriveType == DriveType.Network; // return true if a network drive
}
return true; // is a UNC path
}
Testez le chemin pour voir s'il commence par une barre oblique et si c'est le cas, c'est un chemin UNC. Dans ce cas, vous devez supposer qu'il est un chemin réseau - en réalité, il ne peut pas être un chemin qui pointe vers un autre PC comme il pourrait en théorie être un chemin UNC qui pointe vers votre machine locale, mais ce n'est pas probablement pour la plupart des gens je suppose, mais vous pourriez ajouter des contrôles pour cette condition si vous vouliez une solution plus pare-balles.
Si le chemin ne commence pas par une barre oblique ombles puis utilisez la propriété DriveInfo.DriveType pour déterminer si elle est un lecteur réseau ou non.
Même un chemin UNC (commençant par '\\\') peut pointer vers votre machine locale. – Oded
voir http://stackoverflow.com/questions/520753/what-is-the-correct-way-to-check-if-a-path-is-an-unc-path-or-a-local-path/ 520796 # 520796 – nos
Il est trop tard, mais c'est une réponse parfaite [link], espérons que quelqu'un un [1]: http://stackoverflow.com/questions/2243569/check-if-path-is-on -Réseau # 2244497 – mahmoodels