En base a tu necesidad, yo sugiero que guardes en app.config el prefijo y el consecutivo, te anexo el código de ejemplo para que te des una idea:
Necesita agregar la referencia de: System.Configuration
Imports System.Configuration
El código en el form sería:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try
Dim prefijo, consecutivo As String
prefijo = LeerAppConfig("prefijo")
consecutivo = LeerAppConfig("consecutivo")
Dim cons As Integer = Integer.Parse(IIf(consecutivo = "", "0", consecutivo))
cons = cons + 1
txtFolio.Text = prefijo + "-" + cons.ToString("000") + "/" + DateTime.Now.ToString("dd/MM/yyyy")
''Guardo en app.config
GuardarAppConfig("consecutivo",cons)
Catch ex As Exception
MessageBox.Show(ex.ToString())
End Try
End Sub
private function LeerAppConfig(key As String) as string
Dim result As String = ""
Try
Dim appSettings = ConfigurationManager.AppSettings
result= appSettings(key)
If IsNothing(result) Then
result = "Not found"
End If
Catch e As ConfigurationErrorsException
Console.WriteLine("Error reading app settings")
End Try
return result
End function
private sub GuardarAppConfig(key As String, value As String)
Try
Dim configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
Dim settings = configFile.AppSettings.Settings
If IsNothing(settings(key)) Then
settings.Add(key, value)
Else
settings(key).Value = value
End If
configFile.Save(ConfigurationSaveMode.Modified)
ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name)
Catch e As ConfigurationErrorsException
Console.WriteLine("Error writing app settings")
End Try
End sub
y tu archivo app.config
quedaría de la siguiente manera:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<appSettings>
<add key="prefijo" value="PRE" />
<add key="consecutivo" value="0" />
</appSettings>
</configuration>
Anexo imágenes: