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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| Public Function Lire_Fichier_Texte(ByVal NomFichier As String) As String()
' ##############################################
' ######### Déclaration des variables ##########
' ##############################################
Dim Data(0) As String 'variable retournée par la fonction
Data(0) = "Erreur Texte" 'par défaut elle est en erreur
Dim lines As List(Of String) 'lignes du fichier texte
' ##############################################
' ############ Recherche du fichier ############
' ##############################################
If Not File.Exists(NomFichier) Then
MsgBox("le fichier n'héxiste pas", MsgBoxStyle.Critical = vbOK, "Erreur sur le Fichier Texte")
Data(0) = "Fichier inexistant"
Return Data
End If
' ##############################################
' ########## Lecture du fichier Texte ##########
' ##############################################
Try
lines = File.ReadAllLines(NomFichier).ToList
Catch ex As Exception
MsgBox(ex.ToString, MsgBoxStyle.Critical = vbOK, "Impossible de lire le fichier Texte")
Return Data
End Try
If lines.Count = 0 Then
MsgBox("le fichier est vide", MsgBoxStyle.Critical = vbOK, "Erreur sur le Fichier Texte")
Return Data
End If
' ##############################################
' #### Renvoie les lignes du fichier texte #####
' ##############################################
ReDim Data(lines.Count - 1)
For i As Integer = 0 To lines.Count - 1
Data(i) = lines(i)
Next
Return Data
End Function |
Partager