Les Snippets

Connexion

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

Niveau requis pour utiliser/comprendre cette source : 1 ( Débutant )
Créé le 28/08/2007 21:18:10 et initié par PCPT [Liste]
Date de mise à jour : 24/10/2007 23:24:55
Vue : 16339
Catégorie(s) : API, Chaîne de caractères
Langages dispo pour ce code :
- VB6, VBA
- VB 2005, VB.NET 1.x
- C# 1.x, C# 2.x



Langage : VB6 , VBA
Date ajout : 28/08/2007
Posté par PCPT [Liste]
DateMAJ : 24/10/2007
Private Declare Function DeleteFile Lib "kernel32" Alias "DeleteFileA" (ByVal lpFileName As StringAs Long
Private Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As StringByVal lpszShortPath As String, ByVal lBuffer As Long) As  Long
Private Declare Function MakeSureDirectoryPathExists Lib  "imagehlp.dll" (ByVal lpPath As StringAs Long
'
Function GetShortPath(ByVal sFile As String, Optional bCreate As Boolean = False)  As String
    If bCreate Then
        Call MakeSureDirectoryPathExists(Left$(sFile, InStrRev(sFile, "\")))
        Dim FF As Integer: FF = FreeFile
        Open sFile For Output As #FF
            Print #FF, vbNullString
        Close #FF
        DoEvents
    End If
    
    Dim lRet As Long
    GetShortPath = String(1024Chr$(0))
    lRet = GetShortPathName(sFile, GetShortPath, Len(GetShortPath))
    GetShortPath = Left$(GetShortPath, lRet)
    
    If bCreate Then Call DeleteFile(sFile)
End Function
'
' ========
' EXEMPLE
' ========
'
MsgBox Environ$("PROGRAMFILES") & "\outlook express\un fichier au hasard.txt" & vbCrLf & _
        GetShortPath(Environ$("PROGRAMFILES") & "\outlook express\un fichier  au  hasard.txt"True)

Remarque :
l'API GetShortPathName ne fonctionne qu'avec un fichier/répertoire qui existe. on crée donc un fichier temporaire pour avoir le chemin SHORT
Langage : VB.NET 1.x , VB 2005
Date ajout : 24/10/2007
Posté par Charles Racaud [Liste]

Shared Function GetShortPathName_CreateDestroy(ByVal Path As StringAs String
  Dim fCreate As Boolean = Not System.IO.File.Exists(Path)
  If fCreate Then System.IO.File.Create(Path).Dispose()
  Dim shortPath As String = GetShortPathName(Path)
  If fCreate Then System.IO.File.Delete(Path)
  Return shortPath
End Function
Shared Function GetShortPathName(ByVal Path As StringAs String
  Dim shortPath As New System.Text.StringBuilder(255)
  GetShortPathName(Path, shortPath, shortPath.Capacity)
  Return shortPath.ToString()
End Function
<System.Runtime.InteropServices.DllImport("kernel32.dll", SetLastError:=True, _
  CharSet:=System.Runtime.InteropServices.CharSet.Auto)> _
Private Shared Function GetShortPathName(ByVal Path As String, _
  ByVal ShortPath As System.Text.StringBuilder, _
  ByVal ShortPathLength As IntegerAs Integer
End Function

Remarque :
La fonction GetShortPathName_CreateDestroy fait une vérification si le fichier existe, si non, le fichier est crée pour récupéré son chemin court puis est supprimé.
Tandis que la fonction GetShortPathName ne teste pas son existence. (Renvois "" si il n'existe pas)
Langage : C# 1.x , C# 2.x
Date ajout : 24/10/2007
Posté par Charles Racaud [Liste]
DateMAJ : 24/10/2007

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

Remarque :
La fonction GetShortPathName_CreateDestroy fait une vérification si le fichier existe, si non, le fichier est crée pour récupéré son chemin court puis est supprimé.
Tandis que la fonction GetShortPathName ne teste pas son existence. (Renvois "" si il n'existe pas)

Snippets en rapport avec : Fichier, Chemin, Récupérer, Short, Court



Codes sources en rapport avec : Fichier, Chemin, Récupérer, Short, Court

{JAVA / J2EE} ALGORITHME DE DIJKSTRA
Il s'agit de l'implémentation de l'algorithme de Dijkstra à partir d'un graphe orienté à valuation p...

{PHP} EXEMPLE D'APPLICATION DE L'ALGORITHME DE DIJKSTRA EN PHP
Comme je n'ai pas trouvé d'exemple qui implémente Dijkstra en php je me suis créé un petit programme...

{JAVA / J2EE} DÉTERMINER LE CHEMIN D'UN FICHIER DANS UN RÉPERTOIRE DONNÉ
Il s'agit d'une fonction que retourne le chemin complète "getAbsolutePath" d'un fichier "fileToFind"...

{C / C++ / C++.NET} BELLMAN:LA VALEUR DU PLUS COURT CHEMIN ET LE PLUS COURT CHEMIN
j'ai fait ce code quad j'étais encore débutant en C et je pense qu'il peut servir. Je suis tombé des...

{PHP} RÉCUPÉRER LE TYPE D'UN FICHIER (3 FONCTIONS DIFFÉRENTES)
Bonjour à tous, La plupart des gens pense que récupérer l'extension d'un fichier envoyé par form...

{Visual Basic, VB6, VB.NET, VB 2005} GETNAMES : RÉCUPÈRE ET ÉCRIT TOUS LES NOMS DE FICHIERS D'UN DOSSIER
J'ai fait ce petit programme tout simple, qui aurait pu être créé par n'importe quel débutant, car j...

{Visual Basic, VB6, VB.NET, VB 2005} NTFS RECOVER : RÉCUPÉRER LES FICHIERS EFFACÉS D'UNE PARTITION NTFS
Ce code permet de récupérer les fichiers effacés de vos partitions NTFS. Pour cela, vous devez avoir...

{Visual Basic, VB6, VB.NET, VB 2005} ALGORITHME DE PATHFINDING ASTAR
Cet algorithme célèbre est un algorithme de type PathFinding. Il recherche le chemin le plus rap...

{Delphi} ASTAR - A* RECHERCHE DU PLUS COURT CHEMIN
bonjour, je poste la source d'un A* codé l'an passé. C'était mon premier programme donc je me suis...

{Visual Basic, VB6, VB.NET, VB 2005} CHERCHE LE CHEMIN LE PLUS COURT !
application de l'algorithme GREEDY (cherche un chemin pour arriver au sommet désiré)...