Gerhard Weiss
Gerhard Weiss

Reputation: 9781

Return FileName Only when using OpenFileDialog

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

Upvotes: 24

Views: 71983

Answers (8)

Aladein
Aladein

Reputation: 312

if you want just the selected name without Extension you can try this code

Imports System.IO


PictureNameTextEdit.Text = Path.GetFileNameWithoutExtension(OpenFileDialog1.Fi‌​leName)

thanx

Upvotes: 0

farzaneh
farzaneh

Reputation: 19

C++ code for obtain filename and complete path in OpenFileDialog:

textBox1->Text = OpenFileDialog1->FileName; //complete path textBox1->Text = System::IO::Path::GetFileName(OpenFileDialog1->FileName); //filename

Upvotes: 1

Rzgar
Rzgar

Reputation: 1

Use this code to put the filename in PictureNameTextEdit:

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

Upvotes: -1

frusty
frusty

Reputation: 1

Use SafeFileName instead of FileName and it will return a name (and extension) without path.

Upvotes: -1

Jon Skeet
Jon Skeet

Reputation: 1500525

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

OpenFileDialog.ShowDialog()
PictureNameTextEdit.Text = System.IO.Path.GetFileName(OpenFileDialog.FileName)

Upvotes: 51

MAM
MAM

Reputation: 31

Suppose that I did select word2010 file named as "MyFileName.docx"

This is for ONLY the selected file extension "including the dot mark, f.e (.docx)"

MsgBox(System.IO.Path.GetExtension(Opendlg.FileName))

And this for the selected File name without extension: (MyFileName)

MsgBox(System.IO.Path.GetFileNameWithoutExtension(Opendlg.FileName))

and you can try the other options for the "PATH Class" like: GetFullPath,GetDirectoryName ...and so on.

Upvotes: 0

Jayanthi Murugesan
Jayanthi Murugesan

Reputation: 49

OpenFileDialog.ShowDialog()
PictureNameTextEdit.Text = System.IO.Path.GetFileName(OpenFileDialog.FileName)

Upvotes: 2

virender
virender

Reputation:

//Following code return file name only 

string[] FileFullPath;
string FileName;
objOpenFileDialog.Title = "Select Center Logo";
objOpenFileDialog.ShowDialog();

FileFullPath = objOpenFileDialog.FileNames[0].ToString().Split('\\');
FileName = FileFullPath[FileFullPath.Length - 1]; //return only File Name

//Use following code if u want save other folder , 
// following code save file to CenterLogo  folder which inside bin folder//

System.IO.File.Copy(OFD.FileName, Application.StartupPath + 
"/CenterLogo/" + FileName, true);

Upvotes: -1

Related Questions