How do i get a path to a file when I bring have the UI display a File Explorer

On a UI screen, the user presses a button that displays a File Explorer and chooses a file.
So the program now has a access to the file and its contents.
How can the program know the path from which the file came?

Or, better, a slightly different question.
I want the user to be able to choose a directory (not a specific file). How do i do that?