 |
|
[vb.net]Salvare e aprire file txt
|
|
|
| Diadora |
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
codice: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 |
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 |
le differenze tra vb6 e vb.net sono tante...inoltre l'accesso ai file è
completamente diverso quindi ti posto del codice d'esempio
Scrittura
codice:
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
codice:
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 |
|
|
|
|
|
ALTRI LINK INTERESSANTI
Powered by: Search Engine Indexer and vBulletin v2.3.6
Copyright © 2000 - 2002, Jelsoft Enterprises Limited