PDA

Visualizza la versione completa : [vb.net]Salvare e aprire file txt


Diadora
16-06-2004, 18:33
Salve, sono alle prime armi e devo poter manipolare file txt sia in apertura (per un textbox) sia in salvataggio (per un'altra textbox) e fare sì che questi comandi siano disponibili dal menù a tendina in alto a sinista (questo lo so fare). Inoltre conosco questa funzione di vb6 che dovrebbe fungere anche con il .net

Private Function LeggiFile(File As String) As String

Dim Riga As String

Dim NumFile As Integer

NumFile = FreeFile

Open File For Input As NumFile

While Not EOF(NumFile)

Input #NumFile, Riga

LeggiFile = LeggiFile & Riga & vbCrLf

Wend

Close (NumFile)

End Function
per leggere i file. Il mio problema quindi è
1) come si fa a far funzionare quella funzione, nel senso il menù per scegliere il file che darà il file in pasto alla funzione
2) il salvataggio, per cui brancolo nel buio :D

Sarò lieto se qualcuno mi vorrà aiutare. Grazie mille.

Diadora
17-06-2004, 17:29
Sono riuscito ad aprire il file grazie alla funzione nelle faq della guida, ma nessun mi può aiutare per quanto concerne il salvataggio di un piccolo file txt? :(

giuSp
21-06-2004, 22:22
le differenze tra vb6 e vb.net sono tante...inoltre l'accesso ai file è completamente diverso quindi ti posto del codice d'esempio
Scrittura


Imports System
Imports System.IO

Class Test
Public Shared Sub Main()
' Create an instance of StreamWriter to write text to a file.
Dim sw As StreamWriter = New StreamWriter("TestFile.txt")
' Add some text to the file.
sw.Write("This is the ")
sw.WriteLine("header for the file.")
sw.WriteLine("-------------------")
' Arbitrary objects can also be written to the file.
sw.Write("The date is: ")
sw.WriteLine(DateTime.Now)
sw.Close()
End Sub
End Class

Lettura


Imports System
Imports System.IO

Class Test
Public Shared Sub Main()
Try
' Create an instance of StreamReader to read from a file.
Dim sr As StreamReader = New StreamReader("TestFile.txt")
Dim line As String
' Read and display the lines from the file until the end
' of the file is reached.
Do
line = sr.ReadLine()
Console.WriteLine(Line)
Loop Until line Is Nothing
sr.Close()
Catch E As Exception
' Let the user know what went wrong.
Console.WriteLine("The file could not be read:")
Console.WriteLine(E.Message)
End Try
End Sub
End Class


bye

Loading