- 'API Fichiers
- Const MOVEFILE_REPLACE_EXISTING = &H1
- Const FILE_ATTRIBUTE_TEMPORARY = &H100
- Const FILE_BEGIN = 0
- Const FILE_SHARE_READ = &H1
- Const FILE_SHARE_WRITE = &H2
- Const CREATE_NEW = 1
- Const OPEN_EXISTING = 3
- Const GENERIC_READ = &H80000000
- Const GENERIC_WRITE = &H40000000
-
- Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, ByVal lpOverlapped As Any) As Long
-
- Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
-
- Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
-
- Private Declare Function SetFilePointer Lib "kernel32" (ByVal hFile As Long, ByVal lDistanceToMove As Long, lpDistanceToMoveHigh As Long, ByVal dwMoveMethod As Long) As Long
-
- Private Declare Function GetFileSize Lib "kernel32" (ByVal hFile As Long, lpFileSizeHigh As Long) As Long
-
-
-
- '#############################################
- '# #
- '# Retourne le contenu du fichier "FileName" sous forme d'une String #
- '# #
- '#############################################
-
- 'FileName est le chemin absolue du fichier (par exemple : "c:\toto.doc")
- Public Function OpenFile(ByRef FileName As String) As String
- Dim hOrgFile As Long
- Dim nSize As Long
- Dim Ret As Long
- Dim i As Long
-
- On Error GoTo ErrorHandler
-
- hOrgFile = CreateFile(FileName, _
- GENERIC_READ, _
- FILE_SHARE_READ Or FILE_SHARE_WRITE, _
- ByVal 0&, OPEN_EXISTING, 0, 0)
-
- 'Taille du fichier
- nSize = GetFileSize(hOrgFile, 0)
-
- 'Initialise le pointeur sur le fichier
- SetFilePointer hOrgFile, 0, 0, FILE_BEGIN
-
- OpenFile = Space(nSize)
-
- 'Charge le contenu du fichier dans la variable Openfile
- ReadFile hOrgFile, ByVal OpenFile, nSize, Ret, ByVal 0&
-
- 'Ferme le fichier
- CloseHandle hOrgFile
-
- Exit Function
- ErrorHandler:
- MsgBox "Impossible de lire le fichier "+FileName ,vbExclamation
-
- End Function
'API Fichiers
Const MOVEFILE_REPLACE_EXISTING = &H1
Const FILE_ATTRIBUTE_TEMPORARY = &H100
Const FILE_BEGIN = 0
Const FILE_SHARE_READ = &H1
Const FILE_SHARE_WRITE = &H2
Const CREATE_NEW = 1
Const OPEN_EXISTING = 3
Const GENERIC_READ = &H80000000
Const GENERIC_WRITE = &H40000000
Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, ByVal lpOverlapped As Any) As Long
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function SetFilePointer Lib "kernel32" (ByVal hFile As Long, ByVal lDistanceToMove As Long, lpDistanceToMoveHigh As Long, ByVal dwMoveMethod As Long) As Long
Private Declare Function GetFileSize Lib "kernel32" (ByVal hFile As Long, lpFileSizeHigh As Long) As Long
'#############################################
'# #
'# Retourne le contenu du fichier "FileName" sous forme d'une String #
'# #
'#############################################
'FileName est le chemin absolue du fichier (par exemple : "c:\toto.doc")
Public Function OpenFile(ByRef FileName As String) As String
Dim hOrgFile As Long
Dim nSize As Long
Dim Ret As Long
Dim i As Long
On Error GoTo ErrorHandler
hOrgFile = CreateFile(FileName, _
GENERIC_READ, _
FILE_SHARE_READ Or FILE_SHARE_WRITE, _
ByVal 0&, OPEN_EXISTING, 0, 0)
'Taille du fichier
nSize = GetFileSize(hOrgFile, 0)
'Initialise le pointeur sur le fichier
SetFilePointer hOrgFile, 0, 0, FILE_BEGIN
OpenFile = Space(nSize)
'Charge le contenu du fichier dans la variable Openfile
ReadFile hOrgFile, ByVal OpenFile, nSize, Ret, ByVal 0&
'Ferme le fichier
CloseHandle hOrgFile
Exit Function
ErrorHandler:
MsgBox "Impossible de lire le fichier "+FileName ,vbExclamation
End Function