Récupérer un chemin de fichier au format Short (même si ce fichier n'existe pas)


Contenu du snippet

[System.Runtime.InteropServices.DllImport("kernel32.dll", CharSet=System.Runtime.InteropServices.CharSet.Auto)]

private static extern int GetShortPathName(
  string Path,
  System.Text.StringBuilder ShortPath,
  int ShortPathLength
);

public static string GetShortPathName_CreateDestroy(string Path) {
  bool fCreate = !System.IO.File.Exists(Path);
  if (fCreate)
    System.IO.File.Create(Path).Dispose();
  string shortPath = GetShortPathName(Path);
  if (fCreate)
    System.IO.File.Delete(Path);
  return shortPath;
}

public static string GetShortPathName(string Path) {
  System.Text.StringBuilder shortPath = new System.Text.StringBuilder(255);
  GetShortPathName(Path, shortPath, shortPath.Capacity);
  return shortPath.ToString();
}


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.