9 Replies - 560 Views - Last Post: 17 August 2016 - 06:22 AM Rate Topic: -----

#1 k3nnt0ter0   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 188
  • Joined: 16-January 10

Getting the path of selected files

Posted 17 August 2016 - 02:17 AM

Hi All,

I am in need of your expertise regarding the program I am creating.

Here's how it goes:

I select multiple files then the selected files will return their paths on the rich text box with separator of semicolon. It can only select a single file and I am not sure how I will make the paths separated with semicolon

Here's the concept:
Posted Image

  Dim OpenFileDialog As OpenFileDialog = New OpenFileDialog()
        Dim FileName As String
        OpenFileDialog.Title = "Open File Dialog"
        OpenFileDialog.InitialDirectory = "C:\"
        OpenFileDialog.RestoreDirectory = True
        OpenFileDialog.Multiselect = True
        If OpenFileDialog.ShowDialog() = DialogResult.OK Then
            System.IO.Path.GetFileName(OpenFileDialog.FileName)
            FileName = OpenFileDialog.FileName
            rtbPath.Text = FileName
        End If


I am not practicing any programming language for years so I badly need your help.

This post has been edited by k3nnt0ter0: 17 August 2016 - 02:43 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Getting the path of selected files

#2 maceysoftware   User is offline

  • Foo
  • member icon

Reputation: 353
  • View blog
  • Posts: 1,533
  • Joined: 07-September 13

Re: Getting the path of selected files

Posted 17 August 2016 - 03:22 AM

First off your always going openFileDialog.FileName which will only return the first file name in the list.

If you want to use multiple file names then you need to use the openFileDialog.FileNames

You can then loop around this array and build up your string.

Or

You can use a string function of String.Join whereby you can pass it what you want the separator to be and also an array of string in this case your openFileDialog.FileNames

Do whichever you feel more comfortable doing.
Was This Post Helpful? 0
  • +
  • -

#3 k3nnt0ter0   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 188
  • Joined: 16-January 10

Re: Getting the path of selected files

Posted 17 August 2016 - 04:56 AM

I'm almost there! I tried on a messagebox and it's displaying the array of selected files but I can't join them together in the rich text box. Whenever I press OK button on messagebox, it's resetting the values of richtextbox. How do I concat them?

  Dim OpenFileDialog As OpenFileDialog = New OpenFileDialog()
        Dim LastDir As String = ""
        If LastDir = "" Then Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
        With OpenFileDialog
            .Title = "Open File"
            .InitialDirectory = LastDir
            .Multiselect = True
        End With

        If OpenFileDialog.ShowDialog() = DialogResult.OK Then
            LastDir = System.IO.Path.GetDirectoryName(OpenFileDialog.FileNames.ToString)
            Dim array() As String = OpenFileDialog.FileNames
            For i As Integer = 0 To array.Length - 1
                rtbPath.Text = String.Join(";", array(i))
                MsgBox(array(i))
            Next
        End If

Was This Post Helpful? 0
  • +
  • -

#4 maceysoftware   User is offline

  • Foo
  • member icon

Reputation: 353
  • View blog
  • Posts: 1,533
  • Joined: 07-September 13

Re: Getting the path of selected files

Posted 17 August 2016 - 05:01 AM

' First off, why? OpenFileDialog.FileNames is already a array so why are you putting it in a poorly named array?
          Dim array() As String = OpenFileDialog.FileNames
          For i As Integer = 0 To array.Length - 1
' This is a string.Join function, this will not append the text on.
' a rich textbox has a .append() you may want to try
' However you will have abit of messing to do with the first and last records.

              rtbPath.Text = String.Join(";", array(i))
              MsgBox(array(i))
          Next


Quote

You can then loop around this array and build up your string.

Or

You can use a string function of String.Join whereby you can pass it what you want the separator to be and also an array of string in this case your openFileDialog.FileNames



Not do both.
Was This Post Helpful? 0
  • +
  • -

#5 k3nnt0ter0   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 188
  • Joined: 16-January 10

Re: Getting the path of selected files

Posted 17 August 2016 - 05:24 AM

So I was able to append the files. My only concern now is how do I separate them with semi colon?

        If OpenFileDialog.ShowDialog() = DialogResult.OK Then
            LastDir = System.IO.Path.GetDirectoryName(OpenFileDialog.FileNames.ToString)
            For i As Integer = 0 To OpenFileDialog.FileNames.Length - 1
                rtbPath.AppendText(OpenFileDialog.FileNames(i))
            Next
        End If

Was This Post Helpful? 0
  • +
  • -

#6 maceysoftware   User is offline

  • Foo
  • member icon

Reputation: 353
  • View blog
  • Posts: 1,533
  • Joined: 07-September 13

Re: Getting the path of selected files

Posted 17 August 2016 - 05:40 AM

Well your appending the text are you not?

so surely you would just do the same but this time with a ';'
Was This Post Helpful? 0
  • +
  • -

#7 k3nnt0ter0   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 188
  • Joined: 16-January 10

Re: Getting the path of selected files

Posted 17 August 2016 - 05:53 AM

Thank you! It's almost done. I just want to remove that ";" in the end of that last array. hahaha! But it's not really a problem though.

        If OpenFileDialog.ShowDialog() = DialogResult.OK Then
            LastDir = System.IO.Path.GetDirectoryName(OpenFileDialog.FileNames.ToString)
            For i As Integer = 0 To OpenFileDialog.FileNames.Length - 1
                rtbPath.AppendText(OpenFileDialog.FileNames(i))
                rtbPath.AppendText(";")
            Next
        End If

Was This Post Helpful? 0
  • +
  • -

#8 maceysoftware   User is offline

  • Foo
  • member icon

Reputation: 353
  • View blog
  • Posts: 1,533
  • Joined: 07-September 13

Re: Getting the path of selected files

Posted 17 August 2016 - 06:03 AM

a simple if statement would do this, before you append the ';' check to see if your at the end of the array and if you are then don't append the ';'
Was This Post Helpful? 1
  • +
  • -

#9 k3nnt0ter0   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 188
  • Joined: 16-January 10

Re: Getting the path of selected files

Posted 17 August 2016 - 06:15 AM

View Postmaceysoftware, on 17 August 2016 - 06:03 AM, said:

a simple if statement would do this, before you append the ';' check to see if your at the end of the array and if you are then don't append the ';'


Well Sir, you really are a big help for a non-programmer like me. It's now working. THANK YOU VERY MUCH!
        Dim OpenFileDialog As OpenFileDialog = New OpenFileDialog()
        Dim LastDir As String = ""
        If LastDir = "" Then Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
        With OpenFileDialog
            .Title = "Open File"
            .InitialDirectory = LastDir
            .Multiselect = True
        End With

        If OpenFileDialog.ShowDialog() = DialogResult.OK Then
            LastDir = System.IO.Path.GetDirectoryName(OpenFileDialog.FileNames.ToString)
            For i As Integer = 0 To OpenFileDialog.FileNames.Length - 1
                rtbPath.AppendText(OpenFileDialog.FileNames(i))
                If i <> OpenFileDialog.FileNames.Length - 1 Then
                    rtbPath.AppendText(";")
                Else
                    rtbPath.AppendText("")
                End If
            Next
        End If

This post has been edited by k3nnt0ter0: 17 August 2016 - 06:16 AM

Was This Post Helpful? 0
  • +
  • -

#10 maceysoftware   User is offline

  • Foo
  • member icon

Reputation: 353
  • View blog
  • Posts: 1,533
  • Joined: 07-September 13

Re: Getting the path of selected files

Posted 17 August 2016 - 06:22 AM

Scratch the else, you don't need it.

An if doesn't require a else you can just have a if by itself.

Your welcome.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1