Sie sind auf Seite 1von 3

Visual BASIC 2010 Tips

Centra la form nello schermo


Me.CenterToScreen()

Lettura di un file di testo


Imports System Imports System.IO Try ' Create an instance of StreamReader to read from a file. Using sr As StreamReader = New StreamReader("nome del file da leggere") Dim line As String Do While sr.Peek >= 0 line = sr.ReadLine() [] Loop sr.Close() End Using Catch MsgErr As Exception Select Case Err.Number Case 53 MsgBox("File non trovato") Case Else MsgBox("Si verificato un errore: " & MsgErr.Message) End Select End Try

Scrivere la versione del programma nella caption della form


Me.Text = My.Application.Info.ProductName & " - " & My.Application.Info.Copyright

Split di una riga in base al separatore


line = sr.ReadLine() Dim TestArray() As String = Split(line, ",") Gli elementi della riga saranno contenuti nellarray TestArray(0), TestArray(1), TestArray(2), TestArray(3)

DataGridView - Caricare i dati in una riga


Dim line As String line = sr.ReadLine() Dim TestArray() As String = Split(line, ",") Dim row0 As String() = {TestArray(0), TestArray(1), TestArray(2), TestArray(3)} dgvverbi.Rows.Add(row0)

DataGridView Svuotare una griglia


dgvverbi.Rows.Clear()

Scrivere testo in un file


My.Computer.FileSystem.WriteAllText("C:\TestFolder1\test.txt", "This is new text to be added.",True)

Scrivere una serie di stringhe di testo in un file


For Each foundFile As String In My.Computer.FileSystem.GetFiles("C:\Documents and Settings") foundFile = foundFile & vbCrLf My.Computer.FileSystem.WriteAllText( "C:\Documents and Settings\FileList.txt", foundFile, True) Next

Lettura da file di testo codificato


Dim fileReader As String fileReader = My.Computer.FileSystem.ReadAllText("C:\test.txt", System.Text.Encoding.UTF32) MsgBox(fileReader)

Lettura da file di testo delimitato da virgole


Using MyReader As New Microsoft.VisualBasic. FileIO.TextFieldParser("C:\TestFolder\test.txt") MyReader.TextFieldType = FileIO.FieldType.Delimited MyReader.SetDelimiters(",") Dim currentRow As String() While Not MyReader.EndOfData Try currentRow = MyReader.ReadFields() Dim currentField As String For Each currentField In currentRow MsgBox(currentField) Next Catch ex As Microsoft.VisualBasic. FileIO.MalformedLineException MsgBox("Line " & ex.Message & "is not valid and will be skipped.") End Try End While End Using

Lettura da file di testo a larghezza fissa


Using Reader As New Microsoft.VisualBasic.FileIO.TextFieldParser("C:\TestFolder\test.log")

Reader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.FixedWidth Reader.SetFieldWidths(5, 10, 11, -1) Dim currentRow As String() While Not Reader.EndOfData Try currentRow = Reader.ReadFields() Dim currentField As String For Each currentField In currentRow MsgBox(currentField) Next Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException MsgBox("Line " & ex.Message & "is not valid and will be skipped.") End Try End While End Using

Lettura da file di testo con diversi formati


1.Aggiungere un file di testo denominato testfile.txt al progetto. Aggiungere al file di testo quanto segue: Err Err Acc Err Acc Acc 1001 Cannot access resource. 2014 Resource not found. 10/03/2009User1 Administrator. 0323 Warning: Invalid access attempt. 10/03/2009User2 Standard user. 10/04/2009User2 Standard user.

Dim stdFormat As Integer() = {5, 10, 11, -1} Dim errorFormat As Integer() = {5, 5, -1} Using MyReader As New FileIO.TextFieldParser("..\..\testfile.txt") MyReader.TextFieldType = FileIO.FieldType.FixedWidth MyReader.FieldWidths = stdFormat Dim currentRow As String() While Not MyReader.EndOfData Try Dim rowType = MyReader.PeekChars(3) If String.Compare(rowType, "Err") = 0 Then ' If this line describes an error, the format of the row will be different. MyReader.SetFieldWidths(errorFormat) Else ' Otherwise parse the fields normally MyReader.SetFieldWidths(stdFormat) End If currentRow = MyReader.ReadFields For Each newString In currentRow Console.Write(newString & "|") Next Console.WriteLine() Catch ex As FileIO.MalformedLineException MsgBox("Line " & ex.Message & " is invalid. Skipping") End Try End While End Using Console.ReadLine()

Das könnte Ihnen auch gefallen