|
begin process at 2008 07 06 18:18:29
Derniers logiciels
|
Trouver une ressource (Nouvelle version du moteur, plus rapide & pertinent, essayez le !)
Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum.
Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !
ECRIRE OU LIRE UNE STRUCTURE DANS UN FICHIER BINAIRE
Information sur la source
Description
J'ai cherché un peu mais à ce qu'il me semble, si on souhaite enregistrer une structure dans un fichier binaire (au format .personnalisé) , la meilleur solution reste d'utiliser les méthodes Marshal.StructureToPtr et Marshal.PtrToStructure. Vous trouverez ces méthodes dans 'imports System.Runtime.InteropServices'. Mais il me semble aussi qu'il faut pour cela dimensionner chaque structure correctement afin que 'Marshal' sache la taille de votre structure. Si votre structure ne contient que des valeurs fixes comme des 'integer' , 'double', 'boolean' etc, c'est bon, vous pouvez dimensionner votre structure normalement sans trop vous soucier. Si vous utilisez des string dans vos structures, il faut les limiter à une taille fixe (voir la définition de la structure 'StruPersonne' ci-après)
Source
- Imports System.Runtime.InteropServices
-
- '===================================================================
- '========= Fonctions de Lecture/Ecriture des structures ===========
- '===================================================================
-
-
- Private Function EcrirePerso(ByVal Perso As StruPersonne, ByVal CheminFichier As String) As Boolean
-
- Dim f As New IO.FileStream(CheminFichier , IO.FileMode.OpenOrCreate)
- Try
- EcrirePerso = True
-
-
- Dim vData(Marshal.SizeOf(Perso)) As Byte
- Dim Handle As GCHandle = GCHandle.Alloc(vData, GCHandleType.Pinned)
- Dim Pointer As IntPtr = Handle.AddrOfPinnedObject()
- Marshal.StructureToPtr(Perso, Pointer, False)
- Handle.Free()
-
- f.Write(vData, 0, vData.Length)
-
-
- Catch ex As Exception
- EcrirePerso = False
- Finally 'pour vb.net 2008 seulement, sinon débrouillez vous pour fermer le fichier! :p
- f.Close()
- End Try
- End Function
-
- Private Function LirePerso(ByVal CheminFichier As String) As Boolean
- LirePerso = True
- Try
- Dim vData() As Byte = IO.File.ReadAllBytes(CheminFichier)
- Dim Handle As GCHandle = GCHandle.Alloc(vData, GCHandleType.Pinned)
- Dim Pointer As IntPtr = Handle.AddrOfPinnedObject()
- Moi = DirectCast(Marshal.PtrToStructure(Pointer, GetType(StruPersonne)), StruPersonne)
- Handle.Free()
- Catch ex As Exception
- LirePerso = False
- End Try
-
- End Function
-
-
-
- '===================================================================
- '======================= Module Structures ========================
- '===================================================================
-
-
- Imports System.Runtime.InteropServices
- Public Module Structures
- Public Structure StruPersonne
-
- '-- Strings
- '-- Besoin de dimensionner avec <MarshalAs...
-
- <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=50)> _
- Public NomMonFichier As String
-
- <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=50)> _
- Public Nom As String
-
- <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=50)> _
- Public Prenom As String
-
- <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=50)> _
- Public Email As String
-
- <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=50)> _
- Public Commentaires As String
-
-
- 'Autres structures
- Public Adresse As Adresse
- Public Tel As Téléphone
-
- '-- Pas besoin de dimensionner avec <MarshalAs...
-
- 'Integer
- Public ID As Integer
-
- 'Boolean
- Public IsDead As Boolean
-
- End Structure
-
- Public Structure Adresse
-
- <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=50)> _
- Public Rue As String
-
- <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=50)> _
- Public Ville As String
-
- <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=50)> _
- Public Pays As String
-
- <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=30)> _
- Public CodePostal As String
-
- End Structure
-
- Public Structure Téléphone
- <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=30)> _
- Public Maison As String
- <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=30)> _
- Public Portable As String
- <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=30)> _
- Public Fax As String
- <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=30)> _
- Public Travail As String
- End Structure
-
Imports System.Runtime.InteropServices
'===================================================================
'========= Fonctions de Lecture/Ecriture des structures ===========
'===================================================================
Private Function EcrirePerso(ByVal Perso As StruPersonne, ByVal CheminFichier As String) As Boolean
Dim f As New IO.FileStream(CheminFichier , IO.FileMode.OpenOrCreate)
Try
EcrirePerso = True
Dim vData(Marshal.SizeOf(Perso)) As Byte
Dim Handle As GCHandle = GCHandle.Alloc(vData, GCHandleType.Pinned)
Dim Pointer As IntPtr = Handle.AddrOfPinnedObject()
Marshal.StructureToPtr(Perso, Pointer, False)
Handle.Free()
f.Write(vData, 0, vData.Length)
Catch ex As Exception
EcrirePerso = False
Finally 'pour vb.net 2008 seulement, sinon débrouillez vous pour fermer le fichier! :p
f.Close()
End Try
End Function
Private Function LirePerso(ByVal CheminFichier As String) As Boolean
LirePerso = True
Try
Dim vData() As Byte = IO.File.ReadAllBytes(CheminFichier)
Dim Handle As GCHandle = GCHandle.Alloc(vData, GCHandleType.Pinned)
Dim Pointer As IntPtr = Handle.AddrOfPinnedObject()
Moi = DirectCast(Marshal.PtrToStructure(Pointer, GetType(StruPersonne)), StruPersonne)
Handle.Free()
Catch ex As Exception
LirePerso = False
End Try
End Function
'===================================================================
'======================= Module Structures ========================
'===================================================================
Imports System.Runtime.InteropServices
Public Module Structures
Public Structure StruPersonne
'-- Strings
'-- Besoin de dimensionner avec <MarshalAs...
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=50)> _
Public NomMonFichier As String
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=50)> _
Public Nom As String
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=50)> _
Public Prenom As String
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=50)> _
Public Email As String
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=50)> _
Public Commentaires As String
'Autres structures
Public Adresse As Adresse
Public Tel As Téléphone
'-- Pas besoin de dimensionner avec <MarshalAs...
'Integer
Public ID As Integer
'Boolean
Public IsDead As Boolean
End Structure
Public Structure Adresse
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=50)> _
Public Rue As String
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=50)> _
Public Ville As String
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=50)> _
Public Pays As String
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=30)> _
Public CodePostal As String
End Structure
Public Structure Téléphone
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=30)> _
Public Maison As String
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=30)> _
Public Portable As String
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=30)> _
Public Fax As String
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=30)> _
Public Travail As String
End Structure
Conclusion
Tout ceci reste a confirmer, il y a peut être une façon de créer des fichiers de taille non fixe. si vous avez trouvé dites moi ca m'intéresse.
Historique
- 05 avril 2008 02:28:51 :
- Erreur lors de la création
- 05 avril 2008 10:52:14 :
- Réctification
Sources de la même categorie
Commentaires
Discussions en rapport avec ce code source
|
CalendriCode
| | | L | M | M | J | V | S | D |
| | 1 | 2 | 3 | 4 | 5 | 6 |
| 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| 21 | 22 | 23 | 24 | 25 | 26 | 27 |
| 28 | 29 | 30 | 31 | | | |
|
Téléchargements
Logiciels à télécharger sur le même thème :
|
|