Ciao,un pò di controlli non li mettiamo ? .... e non strutturiamo un pò meglio tutte quelle IF, etc.?
Controllare, migliore le performance e la leggibilità è sempre cosa buona e giusta ;-)
Esempio:
Private Sub Carica_File()
Dim pathPreferenze As String = dirApplicationData & "\Preferenze.tfm"
' Controllo esistenza file Preferenze.tfm
If Not File.Exists(pathPreferenze) Then
MessageBox.Show("File Preferenze.tfm non trovato!", "Errore", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End If
' Lettura percorso cartella Pregeo dal file Preferenze.tfm
Dim readText() As String = File.ReadAllLines(pathPreferenze)
If readText.Length < 3 Then
MessageBox.Show("File Preferenze.tfm non contiene abbastanza righe!", "Errore", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End If
Dim CartPregeoConfig As String = readText(2) & "EXE\"
Dim FilePregeoConfig As String = "Pregeo.config"
Dim pathConfig As String = CartPregeoConfig & FilePregeoConfig
' Controllo esistenza file Pregeo.config
If Not File.Exists(pathConfig) Then
MessageBox.Show("File Pregeo.config non trovato!", "Errore", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End If
' Lettura del file Pregeo.config
Dim righe() As String = File.ReadAllLines(pathConfig)
Me.Text = "Configurazione del file " & pathConfig
Dim PosNomeR As Integer = -1
Dim incr As Integer = 0
For Each currentRow As String In righe
' Ignora righe vuote o commentate con ";"
If String.IsNullOrWhiteSpace(currentRow) OrElse currentRow.StartsWith(";") Then Continue For
incr += 1
If currentRow.IndexOf("[DATI REDATTORE]", StringComparison.OrdinalIgnoreCase) >= 0 Then
PosNomeR = incr + 2
End If
' Trova il carattere '=' nella riga
Dim Posuguale As Integer = InStr(currentRow, "=")
If Posuguale = 0 Then Continue For ' Se non contiene "=", passa alla riga successiva
' Estrarre il valore dopo il "="
Dim valore As String = currentRow.Substring(Posuguale).Trim()
' Assegna i valori ai TextBox corrispondenti
Select Case True
' Tabella Font
Case currentRow.IndexOf("NomeFontPol", StringComparison.OrdinalIgnoreCase) >= 0
TBNomeFontPol.Text = valore
Case currentRow.IndexOf("NumMaxRighePol", StringComparison.OrdinalIgnoreCase) >= 0
TBNumMaxRighePol.Text = valore
Case currentRow.IndexOf("NomeFont", StringComparison.OrdinalIgnoreCase) >= 0
TBNomeFont.Text = valore
Case currentRow.IndexOf("NumMaxRighe", StringComparison.OrdinalIgnoreCase) >= 0
TBNumMaxRighe.Text = valore
' Tabella DISCHI
Case currentRow.IndexOf("DskAPPLexe", StringComparison.OrdinalIgnoreCase) >= 0
TBDskAPPLexe.Text = valore
Case currentRow.IndexOf("DskAPPLada", StringComparison.OrdinalIgnoreCase) >= 0
TBDskAPPLada.Text = valore
Case currentRow.IndexOf("DskAPPLtmp", StringComparison.OrdinalIgnoreCase) >= 0
TBDskAPPLtmp.Text = valore
' Tabella Plus
Case currentRow.IndexOf("Plus", StringComparison.OrdinalIgnoreCase) >= 0
TBPlus.Text = valore
Case currentRow.IndexOf("calcolo esteso", StringComparison.OrdinalIgnoreCase) >= 0
TBCalcoloEsteso.Text = valore
' Tabella Versione
Case currentRow.IndexOf("Versione", StringComparison.OrdinalIgnoreCase) >= 0
TBVersione.Text = valore
Case currentRow.IndexOf("Data", StringComparison.OrdinalIgnoreCase) >= 0
TBData.Text = valore
' Tabella Dati Redattore
Case currentRow.IndexOf("sigla prov.", StringComparison.OrdinalIgnoreCase) >= 0
TBSigla_Prov.Text = valore
Case PosNomeR > 0 AndAlso currentRow.IndexOf("nome", StringComparison.OrdinalIgnoreCase) >= 0
TBNome.Text = valore
Case currentRow.IndexOf("cognome", StringComparison.OrdinalIgnoreCase) >= 0
TBCognome.Text = valore
Case currentRow.IndexOf("qualifica", StringComparison.OrdinalIgnoreCase) >= 0
CBoxQualifica.Text = valore
Case PosNomeR > 0 AndAlso currentRow.IndexOf("provincia", StringComparison.OrdinalIgnoreCase) >= 0
TBProvincia.Text = valore
PosNomeR = -1
Case currentRow.IndexOf("numero", StringComparison.OrdinalIgnoreCase) >= 0
TBNumero.Text = valore
Case currentRow.IndexOf("codc", StringComparison.OrdinalIgnoreCase) >= 0
TBCodiceFiscale.Text = valore
' Tabella LAST PROT
Case currentRow.IndexOf("ultimoprot", StringComparison.OrdinalIgnoreCase) >= 0
TBUltimoProt.Text = valore
' Tabella PROV DI COMPETENZA
Case currentRow.IndexOf("provincia", StringComparison.OrdinalIgnoreCase) >= 0 AndAlso currentRow.Length = 12
TBProvComp.Text = valore
' Tabella DATI TECNICO
Case PosNomeR = -1 AndAlso currentRow.IndexOf("nome", StringComparison.OrdinalIgnoreCase) >= 0
TBNomeTecnico.Text = valore
End Select
Next
End Sub
Dato il tuo file da leggere si avrà che:
Tabella Font
TBNomeFontPol.Text = "COURIER New"
TBNumMaxRighePol.Text = "90"
TBNomeFont.Text = "COURIER New"
TBNumMaxRighe.Text = "65"
Tabella Dischi
TBDskAPPLexe.Text = "C:"
TBDskAPPLada.Text = "C:"
TBDskAPPLtmp.Text = "C:"
Tabella Plus
TBPlus.Text = "S"
TBCalcoloEsteso.Text = "S"
Tabella Versione
TBVersione.Text = "10.00"
TBData.Text = "13/02/2025"
Tabella Dati Redattore
TBSigla_Prov.Text = "XX"
TBNome.Text = "PINCO"
TBCognome.Text = "PALLINO"
CBoxQualifica.Text = "PERITO"
TBProvincia.Text = "XXXXXX"
TBNumero.Text = "5215"
TBCodiceFiscale.Text = "PNCPLL60D12A80L"
Tabella Last Prot
TBUltimoProt.Text = "202545"
Tabella Provincia di Competenza
TBProvComp.Text = "XX" (solo se la riga provincia=XX è lunga esattamente 12 caratteri, altrimenti niente)
Tabella Dati Tecnico
TBNomeTecnico.Text = "PALLINO PINCO"