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 : 4102
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

{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...

{Javascript / DHTML} RECHERCHE DU PLUS COURT CHEMIN - ALGO DE DIJKSTRA - EN JAVASCRIPT
L'algorithme de Dijkstra est aussi appelé algorithme de recherche du plus court chemin, il permet do...

{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é)...

{C / C++ / C++.NET} DECOUPAGE CHEMIN FICHIER (FORUM)
Suite a une demande sur le forum, voila un code qui permet de découper le chemin d'un fichier en so...

{Foxpro} COMMENT ANALYSER LE CHEMIN D'UN FICHIER AVEC WINDOWS SCRIPTING HOST
Le code requiert l'installation de Windows Scripting Host Plateformes supportées: Windows Server...

{ColdFusion} RÉCUPÈRER LE CHEMIN D'UN FICHIER SUR LE SERVEUR DISTANT
L'utilisation de la balise CFFILE nécessite de connaître le chemin absolu pour uploader les fichi...

{Visual Basic, VB6, VB.NET, VB 2005} DRAG & DROP D'UN FICHIER ET EXTRACTION DU NOM ET DU CHEMIN
Ce code reprend une partie de l'exemple de Drag & Drop de la source de goth (voir le lien http://www...