Comment codez-moi l'algorithme ci-dessous dans vb.net?
Procedure logfile() { if "C:\textfile.txt"=exist then open the textfile; else create the textfile; end if go to the end of the textfile; write new line in the textfile; save; close; }
p>
3 Réponses :
Dim FILE_NAME As String = "C:\textfile.txt" Dim i As Integer Dim aryText(4) As String aryText(0) = "Mary WriteLine" aryText(1) = "Had" aryText(2) = "Another" aryText(3) = "Little" aryText(4) = "One" Dim objWriter As New System.IO.StreamWriter(FILE_NAME, True) For i = 0 To 4 objWriter.WriteLine(aryText(i)) Next objWriter.Close() MsgBox("Text Appended to the File") If you set the second parameter to True in the System.IO.StreamWriter's constructor it will append to a file if it already exists, or create a new one if it doesn't.
Il est préférable d'utiliser un composant qui fait ce type de déconnexion de la boîte. Bloc d'application de journalisation de Bibliothèque d'entreprise par exemple. De cette façon, vous obtenez une flexibilité, une évolutivité et vous n'avez pas de contention avec votre fichier journal.
Pour répondre à votre question spécifiquement (désolé, je ne connais pas VB, mais la traduction doit être assez simple) ... p>
Ceci peut également être réalisé en une seule ligne:
System.IO.File.AppendAllText(filePath, "Hello World" & vbCrLf)