Les Snippets

Connexion

Récupérer le chemin du programme associé à un fichier d'après son extension

Niveau requis pour utiliser/comprendre cette source : 1 ( Débutant )
Créé le 21/01/2008 02:02:43 et initié par PCPT [Liste]
Date de mise à jour : 24/07/2008 15:10:55
Vue : 8037
Catégorie(s) : API, Fichier / Disque
Langages dispo pour ce code :
- VB6, VBA
- C# 2.x, C# 3.x
- Delphi 5



Langage : VB6 , VBA
Date ajout : 21/01/2008
Posté par PCPT [Liste]
Private Declare Function DeleteFile Lib "kernel32" Alias "DeleteFileA" (ByVal lpFileName As StringAs Long
Private Declare Function FindExecutable Lib "shell32.dll" Alias "FindExecutableA" (ByVal lpFile As StringByVal lpDirectory As String, ByVal lpResult As String) As Long
Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As LongByVal lpBuffer As String) As Long
Function GetExePathFileFromExtention(ByVal  sExtension As  String) As  String
    Dim sPath As String, lRet As Long, sBuffer As String, FF As Integer
    
'   on récupère le chemin TEMP
    sBuffer = String$(512, vbNullChar)
    lRet = GetTempPath(512, sBuffer)
    sPath = Left$(sBuffer, lRet)
    If Not (RightB$(sPath, 2) = "\"Then sPath = sPath & "\"
'   on crée un fichier  temporaire
    sPath = sPath & Format$(Now"MMDDHHNNSS") & "." & sExtension
    FF = FreeFile
    Open sPath For Output As #FF
        Print #FF, vbNullString
    Close #FF
'   on récupère l'exe associé
    sBuffer = String$(260, vbNullChar)
    lRet = FindExecutable(sPath, vbNullString,  sBuffer)
'   retour
    If lRet > 32 Then
        GetExePathFileFromExtention = Left$(sBuffer, InStr(sBuffer, vbNullChar) -  1)
    Else
        GetExePathFileFromExtention = vbNullString
    End If
    
'   supprime fichier  temp
    Call DeleteFile(sPath)
End Function

Langage : C# 2.x , C# 3.x
Date ajout : 03/07/2008
Posté par MorpionMx [Liste]
DateMAJ : 03/07/2008
[DllImport("kernel32.dll")] 
static extern uint GetTempPath(uint nBufferLength, [Out] StringBuilder lpBuffer);[
DllImport("shell32.dll", EntryPoint="FindExecutable")]
static extern long FindExecutableA(string lpFile, string lpDirectory, [Out] StringBuilder lpResult); 
public static string GetExePathFileFromExtention(string extension) 
{

    string tempFileName = Path.ChangeExtension(Path.GetTempFileName(), extension);
    string tempPathFile = Path.Combine("temp", tempFileName); 
    StringBuilder sb = new StringBuilder(1024);
    File.Create(tempFileName); 
    long ret = FindExecutableA(tempFileName, string.Empty, sb);
    if (ret >= 32) return sb.ToString(); 
    else return string.Empty; 
}


Remarque :
Avec les directives using qui vont bien :
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
Langage : Delphi 5
Date ajout : 24/07/2008
Posté par cirec [Liste]
DateMAJ : 24/07/2008
function GetExePathFileFromExtention(const aExt: String): String;  
var sTmpPath, sTmpName: String; 
    iLen: Integer; 
begin 
  SetLength(Result, MAX_PATH);  
  SetLength(sTmpPath, MAX_PATH); 
  iLen := GetTempPath(MAX_PATH,  PChar(sTmpPath)); 
  SetLength(sTmpPath, iLen); 
  sTmpName := sTmpPath +  FormatDateTime('MMDDHHNNSS', Now); 
  if aExt[1] = '.' then 
    sTmpName  := sTmpName + aExt 
  else 
    sTmpName  := sTmpName + '.' + aExt; 
  with TFileStream.Create(sTmpName, fmCreate or fmShareDenyWrite) do  
  try 
    iLen := FindExecutable(PChar(sTmpName), nil, PChar(Result)); 
  finally 
     Free; 
    DeleteFile(sTmpName);  
    if iLen < 32 then 
       Result  := EmptyStr; 
  end; 
end; 
{Utilisation} 
procedure  Tfrm_Main.Button1Click(Sender: TObject); 
begin 
  Label1.Caption :=  GetExePathFileFromExtention('Txt'); 
end; 

Remarque :
Ne pas oublier d'ajouter l'unité "ShellApi" dans la clause "Uses"

Snippets en rapport avec : Fichier, Chemin, Programme, Extension, Récupérer



Codes sources en rapport avec : Fichier, Chemin, Programme, Extension, Récupérer

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

{Delphi} MODIFICATION DES EXTENSIONS DE FICHIERS
Ceci est un petit utilitaire qui m'est très utile dans mon travail. Il permet de modifier, ajouter o...

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

{PHP} PARCOURIR UN RÉPERTOIRE ET SES SOUS SOUS SOUS... RÉPERTOIRE ET RETOURNER LES FICHIERS QUI ONT L'EXTENSION VOULUE
Cette fonction va parcourir tous les répertoires et sous répertoires et sous sous répertoires et ......

{Visual Basic, VB6, VB.NET, VB 2005} OUVRIR UN FICHIER AVEC SON PROGRAMME PAR DÉFAUT
Voila enfin LA réponse à tous ceux qui ne savent pas comment ouvrir un fichier avec son programme wi...

{PHP} TYPE DU FICHER
Cette petite fonction toute simple vous determine l'extension (et donc le type) d'un fichier. Il vo...

{SQL} SQL SERVER - COMMENT OBTENIR L'EXTENSION D'UN FICHIER DEPUIS L'URL
Dans bien des cas, on stocke une URL permettant d'accéder à un fichier dans la base de données. Le ...

{PHP} TRI PAR TYPE DE FICHIER / EXTENSION
Fonction pour trier des noms de fichiers par type (et alphabétiquement au sein d'un type). Concrè...

{Visual Basic, VB6, VB.NET, VB 2005} FICHIER TRAITEMENT EN BLOC : RECHERCHER-REMPLACER ET MODIFICATION D'EXTENSION
Lorsque l'on réalise des pages PHP ou Html(Surtout Html), modifier une chaine de caractère dans tous...