Buona sera, volevo chiedere un consiglio, sto realizzando un'applicazione di prova per gestire un file sequenziale, la lettura è perfetta, infatti il testo contenuto viene messo in un TextBox (TbxTesto), i problemi nascono quando eseguo le funzioni di scrittura o di cancellazione, in quanto la funzione rimane appesa per tanti secondi (troppi) ed alcune volte poi finisce regolarmente, altre volte la cancello.
Capisco che la domanda e l'esposizione è generica, ma dove potrei guardare per risolvere il problema?
Segue l'applicativo incriminato
Imports System.IO
'---------------------------------------------------------------------------------------------------------------------------------------------' Applicazione di prova: Gestione testi su file sequenziale
'---------------------------------------------------------------------------------------------------------------------------------------------
Public Class Form1
Const fileSeq As String = "F:\VbasicMio\4 - Lavori VB\FileProvaSeq.txt"
Dim linee As New List(Of String)
Dim linea As String
Dim qtarec As Integer
Dim erro As Boolean
Private Sub BtnEsegui_Click(sender As Object, e As EventArgs) Handles BtnEsegui.Click
erro = False
qtarec = 0
LblMsg.Text = ""
If RbtLeggere.Checked = False And RbtScrivere.Checked = False And RbtDaZero.Checked = False And RbtCancellare.Checked = False Then
LblMsg.Text = "Obbligatorio selezionare l'operazione desiderata"
Exit Sub
End If
If RbtLeggere.Checked = True Then
Call LeggiFile()
If erro Then
Exit Sub
End If
Else
If RbtScrivere.Checked = True Then
Call ScriviFileAggiungendo()
If erro Then
Exit Sub
End If
Else
If RbtDaZero.Checked = True Then
Call ScriviFileDaZero()
If erro Then
Exit Sub
End If
Else
If RbtCancellare.Checked = True Then
Call CancellaFile()
If erro Then
Exit Sub
End If
End If
End If
End If
End If
End Sub
Private Sub LeggiFile()
TbxTesto.Text = ""
linea = ""
linee.Clear()
If Not IO.File.Exists(fileSeq) Then
erro = True
LblMsg.Text = "Il file "& fileSeq & " NON esiste - bisogna crearlo"
Exit Sub
End If
Using reader As New StreamReader(fileSeq)
linea = reader.ReadLine 'legge la prima linea
qtarec += 1
Do Until linea Is Nothing
linee.Add(linea)
linea = reader.ReadLine 'legge la linea successiva
qtarec += 1
Loop
reader.Close()
End Using
TbxTesto.Lines = linee.ToArray
LblMsg.Text = "Il file "& fileSeq & "è stato letto - è composto da "& qtarec - 1 & " records"
RbtLeggere.Checked = False
End Sub
Private Sub ScriviFileAggiungendo()
If TbxNuovo.TextLength < 1 Then
LblMsg.Text = "Il nuovo testo NON è stato digitato"
Exit Sub
End If
Try
Using writer As New StreamWriter(fileSeq, True)
writer.WriteLine(TbxNuovo.Text)
writer.Flush()
writer.Close()
End Using
Catch ex As Exception
LblMsg.Text = "Errore: "& ex.Message
End Try
LblMsg.Text = "Nel file "& fileSeq & "è stato aggiunto il nuovo testo digitato"
TbxNuovo.Text = ""
TbxTesto.Text = ""
linea = ""
RbtScrivere.Checked = False
End Sub
Private Sub ScriviFileDaZero()
If TbxNuovo.TextLength < 1 Then
LblMsg.Text = "Il nuovo testo NON è stato digitato"
Exit Sub
End If
Try
Using writer As New StreamWriter(fileSeq, False)
writer.WriteLine(TbxNuovo.Text)
writer.Flush()
writer.Close()
End Using
Catch ex As Exception
LblMsg.Text = "Errore: "& ex.Message
End Try
LblMsg.Text = "Nel file "& fileSeq & "è stato scritto il nuovo testo digitato, perdendo quello precedente"
TbxNuovo.Text = ""
TbxTesto.Text = ""
linea = ""
RbtDaZero.Checked = False
End Sub
Private Sub CancellaFile()
File.Delete(fileSeq)
LblMsg.Text = "Il file "& fileSeq & "è stato cancellato"
TbxTesto.Text = ""
RbtCancellare.Checked = False
End Sub
Private Sub RbtScrivere_CheckedChanged(sender As Object, e As EventArgs) Handles RbtScrivere.CheckedChanged
LblMsg.Text = "Scrivere il testo che verrà aggiunto a quello attuale"
TbxNuovo.Focus()
End Sub
Private Sub RbtDaZero_CheckedChanged(sender As Object, e As EventArgs) Handles RbtDaZero.CheckedChanged
LblMsg.Text = "Scrivere il testo che sostituirà quello attuale (i dati verranno persi)"
TbxNuovo.Focus()
End Sub
End Class