getule Posted November 21, 2011 Share Posted November 21, 2011 Il est presque toujours nécessaire lors du développement d'un site web de pouvoir écrire et lire sur des fichiers. La procédure pour y arriver est relativement simple avec vb.net et c#. Il suffit de faire appel à la classe à deux classes StreamReader et StreamWriter de l'espace de noms System.IO StreamReader permet de lire un flux de caractères depuis un fichier. StreamWriter permet d'écrire un flux de caractères sur un fichier. Passons au code: Tout d'aobord importons System.IO Imports System.IO Procédure de lecture: Sub LireFichier() Dim MonSR As StreamReader 'déclaration d'une instance StreamReader Try MonSR = File.OpenText(Server.MapPath("Monfichier.txt")) 'ouverture du fichier Me.Literal1.Text =MonSR.ReadToEnd ' affichage des données du fichier dans un Literal MonSR.Close() 'Fermeture du StreamReader (à ne pas oublier) Catch ex As Exception End Try End Sub Procédure d'écriture: Sub EcrireFichier() Dim MonSR As StreamWriter = New StreamWriter(Server.MapPath("Monfichier.txt")) Try MonSR.Write(Me.TextBox1.Text) 'Ecriture du contenu du textbox sur mon fichier MonSR.Close() Catch ex As Exception End Try End Sub et voila, source: http://forum.diafdz.com/yaf_postst23_Asp-net--comment-lire-et-ecrire-sur-un-fichier-text-avec-VB-net.aspx Quote Link to comment Share on other sites More sharing options...
Darkvader Posted November 24, 2011 Share Posted November 24, 2011 I think you're not handling exceptions really well here especially with file manipulation. Let's take the code below, if for some reason the File.OpenText or ReadToEnd throws an exception your MonSR.Close will never be reached and will keep the stream in an invalid state. Another thing is to always initialize variables before using them, it's a good practice and it helps with debugging. Sub LireFichier() Dim MonSR As StreamReader 'déclaration d'une instance StreamReader Try MonSR = File.OpenText(Server.MapPath("Monfichier.txt")) 'ouverture du fichier Me.Literal1.Text =MonSR.ReadToEnd ' affichage des données du fichier dans un Literal MonSR.Close() 'Fermeture du StreamReader (à ne pas oublier) Catch ex As Exception End Try End Sub The best way to do this is to Close the file handle in a Finally block like below and it sure will free the stream. Sub LireFichier() Dim MonSR As StreamReader = Nothing 'déclaration d'une instance StreamReader Try MonSR = File.OpenText(Server.MapPath("Monfichier.txt")) 'ouverture du fichier Me.Literal1.Text =MonSR.ReadToEnd ' affichage des données du fichier dans un Literal Catch ex As Exception Finally If MonSR IsNot Nothing Then MonSR.Close() End If End Try End Sub Same below, if the Write fails we make sure to close the handle to the file Sub EcrireFichier() Dim MonSR As StreamWriter = New StreamWriter(Server.MapPath("Monfichier.txt")) Try MonSR.Write(Me.TextBox1.Text) 'Ecriture du contenu du textbox sur mon fichier Catch ex As Exception Finally If MonSR IsNot Nothing Then MonSR.Close() End If End Try End Sub Happy coding. Quote Link to comment Share on other sites More sharing options...
getule Posted November 24, 2011 Author Share Posted November 24, 2011 (edited) vous avez effectivement raison d'instancier le nouveau objet StreamReader et StreamWriter à l'interieur de try car pour une quelconque raison qui empêche la liaison au fichier, l'exception n'est pas portée. to^^^^ois, vous n'avez pas raison quand vous posez une condition pour finally alors que le catch permet ceci directement. donc je me rectifie : [left][color=#000000][font=Consolas]Sub LireFichier()[/font][/color] [color=#000000][font=Consolas] Dim MonSR As StreamReader = Nothing 'déclaration d'une instance StreamReader[/font][/color] [color=#000000][font=Consolas] Try[/font][/color] [color=#000000][font=Consolas] MonSR = File.OpenText(Server.MapPath("Monfichier.txt")) 'ouverture du fichier[/font][/color] [color=#000000][font=Consolas] Me.Literal1.Text =MonSR.ReadToEnd ' affichage des données du fichier dans un Literal[/font][/color] [color=#000000][font=Consolas] MonSR.Close() 'Fermeture du StreamReader (à ne pas oublier)[/font][/color] [color=#000000][font=Consolas] Catch ex As Exception[/font][/color] [color=#000000][font=Consolas] [/font][/color] [color=#000000][font=Consolas] End Try[/font][/color] [color=#000000][font=Consolas] End Sub[/font][/color][/left] et pour l’écriture: [left][color=#000000][font=Consolas]Sub EcrireFichier()[/font][/color] [color=#000000][font=Consolas] Try[/font][/color] [color=#000000][font=Consolas] Dim MonSR As StreamWriter = New StreamWriter(Server.MapPath("Monfichier.txt"))[/font][/color] [color=#000000][font=Consolas] MonSR.Write(Me.TextBox1.Text) 'Ecriture du contenu du textbox sur mon fichier[/font][/color] [color=#000000][font=Consolas] MonSR.Close()[/font][/color] [color=#000000][font=Consolas] Catch ex As Exception[/font][/color] [color=#000000][font=Consolas] [/font][/color][color=#000000][font=Consolas] End Try[/font][/color] [color=#000000][font=Consolas] End Sub[/font][/color][/left] Edited January 6, 2012 by getule Quote Link to comment Share on other sites More sharing options...
Darkvader Posted November 24, 2011 Share Posted November 24, 2011 (edited) Whoever looks at your code will notice the redundant code you added in the exception block. As I explained, the code inside Finally will execute either there is an exception or not, there is no need to add a second Close. BTW the compiler will optimize your code and will get rid of your second Close anyway. Edited November 24, 2011 by Darkvader Quote Link to comment Share on other sites More sharing options...
getule Posted January 6, 2012 Author Share Posted January 6, 2012 c'est vrai, t'as raison. merci Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.