String Manipulation in VB.Net

Asked

Viewed 245 times

1

Hello, in the code I’m developing I need to get the name of a file. 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 bar ("Image.png"), but I don’t know how to do it... I tried so many ways and I couldn’t.

Below follows the Openfiledialog Code (this 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 follows the code I quoted above (what I can’t 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 take the file name " Image.png" from the string "C: Users Lucas Documents Image.png"

2 answers

1

Utilize:

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

The method System.IO.Path.GetFileName 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 here.

0

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

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

See working on rextester.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.