String handling in VB.Net

1

Hello, in the code I'm developing I need to get a file name. I used an OpenFileDialog for the user to select a file, and after selecting, the system returns the directory of this file within a string. Example: "C: \ Users \ Lucas \ Documents \ Image.png".

But I need to copy this file to the project folder (Application.StartupPath) however, for this I need to get the name of this file from the slash ("\ Image.png"), but I do not know how to do it, I tried it in different ways and I could not.

Below is the OpenFileDialog Code (it works):

Private Sub btnProcurar_Click(sender As Object, e As EventArgs) Handles btnProcurar.Click
    Dim OFD As New OpenFileDialog()
    OFD.Filter = "Imagens (*.PNG; *.JPG)|*.PNG;*.JPG|" & "All files (*.*)|*.*"
    OFD.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal)
    OFD.FilterIndex = 1
    OFD.Multiselect = False
    OFD.Title = "Selecionar Comprovante de Depósito"
    If (OFD.ShowDialog() = DialogResult.OK) Then
        NomeArquivo = OFD.FileName
        txtCaminhoComprovante.Text = NomeArquivo
    End If
End Sub

Below is the code I mentioned above (which I can not do):

Private Sub btnConfirmar_Click(sender As Object, e As EventArgs) Handles btnConfirmar.Click
    If txtValor.TextLength < 4 Then
        MessageBox.Show("O valor informado no campo deve ter no mínimo quatro caracteres! Exemplo: 0.00", "FinanSys - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
    Else
        If txtCaminhoComprovante.TextLength < 1 Then
            MessageBox.Show("Você deve selecionar um comprovante do depósito informado!", "FinanSys - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Else
            If File.Exists(NomeArquivo) Then
                Dim StringReversa As String = StrReverse(NomeArquivo)
                Dim Barra As String = Asc(92)
                Dim CaracterObtido As String = ""
                For i As Integer = 0 To StringReversa.Length - 1
                    If String.Compare(CaracterObtido = StringReversa.Substring(i, 1), Barra) Then
                        CaracterObtido = StringReversa.Substring(i, 1)
                        Exit For
                    End If
                Next
                //FileCopy(NomeArquivo, Application.StartupPath)
                MsgBox(CaracterObtido)
            Else
                        MessageBox.Show("O comprovante selecionado não existe!", "FinanSys - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        End If
    End If
End Sub

In short, what I need is to get the file name "\ Image.png" from the string "C: \ Users \ Lucas \ Documents \ Image.png"

    
asked by anonymous 03.09.2017 / 20:50

2 answers

1

Use:

Dim filename As String = System.IO.Path.GetFileName(fullpath)

The System.IO.Path.GetFileName method returns the file name of a string containing a complete directory, which is exactly what you want. Example:

Dim diretorioExemplo As String = "C:\PastaExemplo\ArquivoExemplo.exe"
Dim arquivoExemplo As String = "\" & System.IO.Path.GetFileName(diretorioExemplo)
''arquivoExemplo vai retornar "\ArquivoExemplo.exe"

If you want more examples, look at here .

    
03.09.2017 / 22:43
0

I believe you can use .Split() to do this, see how it would look:

Dim nomeArquivoArray() As String = NomeArquivo.Split("\")
nomeArquivo = nomeArquivoArray(nomeArquivoArray.Count - 1)

See working at rextester .

    
03.09.2017 / 22:37