Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return FileName Only when using OpenFileDialog

Tags:

I am using the following method to browse for a file:

    OpenFileDialog.ShowDialog()     PictureNameTextEdit.Text = OpenFileDialog.FileName 

Is there a way get ONLY the file name?

The FileName method returns the entire path and file name.

i.e. I want Foo.txt instead of C:\SomeDirectory\Foo.txt

like image 553
Gerhard Weiss Avatar asked Dec 04 '08 22:12

Gerhard Weiss


People also ask

How do I get the selected file from OpenFileDialog box?

OpenFileDialog component opens the Windows dialog box for browsing and selecting files. To open and read the selected files, you can use the OpenFileDialog. OpenFile method, or create an instance of the System. IO.

How do I prompt a user to select a file in Python?

Use the askopenfilename() function to display an open file dialog that allows users to select one file. Use the askopenfilenames() function to display an open file dialog that allows users to select multiple files.


2 Answers

Use Path.GetFileName(fullPath) to get just the filename part, like this:

OpenFileDialog.ShowDialog() PictureNameTextEdit.Text = System.IO.Path.GetFileName(OpenFileDialog.FileName) 
like image 65
Jon Skeet Avatar answered Sep 22 '22 14:09

Jon Skeet


OpenFileDialog.ShowDialog() PictureNameTextEdit.Text = System.IO.Path.GetFileName(OpenFileDialog.FileName) 
like image 29
Jayanthi Murugesan Avatar answered Sep 19 '22 14:09

Jayanthi Murugesan