Reputation: 9781
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
Reputation: 312
if you want just the selected name without Extension you can try this code
Imports System.IO
PictureNameTextEdit.Text = Path.GetFileNameWithoutExtension(OpenFileDialog1.FileName)
thanx
Upvotes: 0
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
Reputation: 1
Use this code to put the filename in PictureNameTextEdit:
OpenFileDialog.ShowDialog()
PictureNameTextEdit.Text = OpenFileDialog.SafeFileName
Upvotes: -1
Reputation: 1
Use SafeFileName instead of FileName and it will return a name (and extension) without path.
Upvotes: -1
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
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
Reputation: 49
OpenFileDialog.ShowDialog()
PictureNameTextEdit.Text = System.IO.Path.GetFileName(OpenFileDialog.FileName)
Upvotes: 2
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