OVH Cloud OVH Cloud

Obtenir un nom de fichier temporaire

9 réponses
Avatar
Frédéric LAMBOUR
Comment obtenir (facilement) un nom de fichier unique (pour un stockage
temporaire) ?

9 réponses

Avatar
Cédric Naudy
Guidgen.exe (tool de VS.NET) génère des GUID qui sont uniques dans le monde.

Vous pouvez l'utiliser pour ça.

Cédric


"Frédéric LAMBOUR" wrote in message
news:
Comment obtenir (facilement) un nom de fichier unique (pour un stockage
temporaire) ?




Avatar
Ghislain Proulx
Bonjour Frédéric,

Voici une méthode qui utilise des appels API mais qui fonctionne très bien
(pour moi du moins).

Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA"
(ByVal nBufferLength As Integer, ByVal lpBuffer As String) As Integer
Private Declare Function GetTempFileName Lib "kernel32" Alias
"GetTempFileNameA" (ByVal lpszPath As String, ByVal lpPrefixString As
String, ByVal wUnique As Integer, ByVal lpTempFileName As String) As Integer

Public Function GetTempPath() As String
Dim r As Int32
Dim sWinTmpDir As String

'get the user's windowstemp folder pad the passed string
sWinTmpDir = Space$(MAX_PATHTempPath)
'get the folder
r = GetTempPath(CInt(MAX_PATHTempPath), sWinTmpDir)
'r contains the number of chrs up to the terminating null, so a simple
left$ can be used. Its also conveniently terminated with a slash.
sWinTmpDir = Left$(sWinTmpDir, CInt(r))
Return sWinTmpDir
End Function

Public Function GetTempFileName() As String
Dim r As Int32
Dim sTmpFile As String
Dim sWinTmpDir As String = GetTempPath

'pad a working string
sTmpFile = Space$(MAX_PATHTempPath)

'call the API.
'The first param is the path in which to create the file. Passing "."
creates the file in the current directory.
'A specific path can also be passed. Using the GetTempPath() API (as in
Form Load) returns Windows temporary folder, which is used here.
'the second param is the prefix string (note: null-terminated under NT).
'The function uses up to the first three characters of this string as
the prefix of the filename. This string must consist of characters in the
ANSI character set.
'the third parameter, uUnique, specifies an unsigned integer that the
function converts to a hexadecimal string for use in creating the temporary
filename.
'If uUnique is nonzero, the function appends the hexadecimal string to
lpPrefixString to form the temporary filename.
'In this case, the function does not create the specified file, and does
not test whether the filename is unique.
'If uUnique is zero, as below, the function uses a hexadecimal string
derived from the current system time.
'In this case, the function uses different values until it finds a
unique filename, and then it creates the file in the lpPathName directory.
'the last param is the variable to contain the temporary filename,
null-terminated and consisting of characters in the ANSI character set.
'This string should be padded at least the length, in bytes, specified
by MAX_PATH to accommodate the path.
r = GetTempFileName(sWinTmpDir, "tmp", 0, sTmpFile)

'If the function succeeds, the return value 'r' specifies the unique
numeric value (in decimal) used in the temporary filename.

'If the function fails, the return value is zero.
If r <> 0 Then
'strip the trailing null
sTmpFile = Left$(sTmpFile, InStr(sTmpFile, Chr(0)) - 1)
End If

Return sTmpFile
End Function

Bonne journée

Ghislain Proulx


"Frédéric LAMBOUR" a écrit dans le message
de news:
Comment obtenir (facilement) un nom de fichier unique (pour un stockage
temporaire) ?




Avatar
Zoury
Il y a aussi Path.GetTempFileName() qui créer un fichier vide ayant un nom
unique dans le répertoire temporaire (Path.GetTempPath()) et te renvoit le
nom de ce fichier.

--
Cordialement
Yanick Lefebvre - MVP pour Visual Basic classique
http://faq.vb.free.fr/?rubrique=0 - http://www.mvps.org/vbnet/
http://www.mentalis.org/agnet/apiguide.shtml - http://www.mztools.com/
Avatar
Pierre Alexis [MVP]
Salut Frédéric,

Tu as écrit :

Comment obtenir (facilement) un nom de fichier unique (pour un stockage
temporaire) ?



Plus simple que ça tu meurs ;-) :

System.IO.Path.GetTempFileName()

Description : "Retourne un nom de fichier temporaire unique et crée un fichier
de zéro octet avec ce nom sur le disque."

Enjoy !

--
Pierre ALEXIS - MVP Visual Basic Classique
Courriel & messenger :
FAQ Visual Basic : http://faq.vb.free.fr/
Avatar
Zoury
Salut Ghislain! :O)

Voici une méthode qui utilise des appels API
Private Declare Function GetTempPath
Private Declare Function GetTempFileName



Ces deux méthodes ont été créer dans le Framework. Elles sont disponibles
depuis la classe Path.

--
Cordialement
Yanick Lefebvre - MVP pour Visual Basic classique
http://faq.vb.free.fr/?rubrique=0 - http://www.mvps.org/vbnet/
http://www.mentalis.org/agnet/apiguide.shtml - http://www.mztools.com/
Avatar
Pierre Alexis [MVP]
Salut Ghislain,

Tu as écrit :

Voici une méthode qui utilise des appels API mais qui fonctionne très bien
(pour moi du moins).



Attention, ce n'est pas du code managé ;-) La méthode que je propose, elle par
contre, est définie dans la norme CLI (Common Language Infrastructure).

--
Pierre ALEXIS - MVP Visual Basic Classique
Courriel & messenger :
FAQ Visual Basic : http://faq.vb.free.fr/
Avatar
Ghislain Proulx
Oups... J'ai fais dans le compliqué moi là !!! :-)

Merci du conseil !

Ghislain Proulx

"Pierre Alexis [MVP]" a écrit dans le message de
news:%
Salut Frédéric,

Tu as écrit :

> Comment obtenir (facilement) un nom de fichier unique (pour un stockage
> temporaire) ?

Plus simple que ça tu meurs ;-) :

System.IO.Path.GetTempFileName()

Description : "Retourne un nom de fichier temporaire unique et crée un


fichier
de zéro octet avec ce nom sur le disque."

Enjoy !

--
Pierre ALEXIS - MVP Visual Basic Classique
Courriel & messenger :
FAQ Visual Basic : http://faq.vb.free.fr/




Avatar
Frédéric LAMBOUR
Ok c'est ça que je cherchais.

"Pierre Alexis [MVP]" a écrit dans le message de
news:%
Salut Frédéric,

Tu as écrit :

> Comment obtenir (facilement) un nom de fichier unique (pour un stockage
> temporaire) ?

Plus simple que ça tu meurs ;-) :

System.IO.Path.GetTempFileName()

Description : "Retourne un nom de fichier temporaire unique et crée un


fichier
de zéro octet avec ce nom sur le disque."

Enjoy !

--
Pierre ALEXIS - MVP Visual Basic Classique
Courriel & messenger :
FAQ Visual Basic : http://faq.vb.free.fr/




Avatar
Stéphane L.
Merci, ça m'est aussi utile :)

Stéphane


"Pierre Alexis [MVP]" a écrit dans le message de
news:%
Salut Frédéric,

Tu as écrit :

> Comment obtenir (facilement) un nom de fichier unique (pour un stockage
> temporaire) ?

Plus simple que ça tu meurs ;-) :

System.IO.Path.GetTempFileName()

Description : "Retourne un nom de fichier temporaire unique et crée un


fichier
de zéro octet avec ce nom sur le disque."

Enjoy !

--
Pierre ALEXIS - MVP Visual Basic Classique
Courriel & messenger :
FAQ Visual Basic : http://faq.vb.free.fr/