Corriger la casse d'un chemin d'accès

Contenu du snippet

const int MAX_PATH = 260;
const int S_OK = 0x0;

[DllImport("shell32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
static extern bool SHGetPathFromIDList(IntPtr pidl, StringBuilder pszPath);
[DllImport("shell32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
static extern int SHParseDisplayName([MarshalAs(UnmanagedType.LPWStr)] string pszName, IntPtr pbc, out IntPtr ppidl, uint sfgaoIn, out uint psfgaoOut);

static string FixPathCase(string path) {
  StringBuilder newPath = new StringBuilder(MAX_PATH);
  IntPtr ppidl;
  uint psfgaoOut;
  int result = SHParseDisplayName(path, IntPtr.Zero, out ppidl, 0U, out psfgaoOut);
  if (result == S_OK) {
    bool r = SHGetPathFromIDList(ppidl, newPath);
    Marshal.FreeCoTaskMem(ppidl);
    if (!r)
      throw new Win32Exception(Marshal.GetLastWin32Error());
  }
  else
    throw new Win32Exception(result);
  return newPath.ToString();
}

Compatibilité : C# 1.x, C# 2.x, C# 3.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.