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) ?
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) ?
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" <PasDeSpam_f.LAMBOUR@everlog.com> a écrit dans le message
de news:OBmhdCWeEHA.3988@tk2msftngp13.phx.gbl...
Comment obtenir (facilement) un nom de fichier unique (pour un stockage
temporaire) ?
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) ?
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.
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.
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.