vb.net 仅在使用 OpenFileDialog 时返回 FileName

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/342268/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 13:56:50  来源:igfitidea点击:

Return FileName Only when using OpenFileDialog

vb.net

提问by Gerhard Weiss

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 FileNamemethod returns the entire path and file name.

文件名的方法返回整个路径和文件名。

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

即我想要 Foo.txt 而不是 C:\SomeDirectory\Foo.txt

回答by Jon Skeet

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

用于Path.GetFileName(fullPath)仅获取文件名部分,如下所示:

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

回答by Jayanthi Murugesan

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

回答by farzaneh

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

在 OpenFileDialog 中获取文件名和完整路径的 C++ 代码:

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

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

回答by Aladein

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

谢谢

回答by MAM

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

假设我确实选择了名为“MyFileName.docx”的 word2010 文件

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

这仅适用于选定的文件扩展名“包括点标记,fe (.docx)”

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

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

这对于没有扩展名的选定文件名:(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.

您可以尝试“PATH 类”的其他选项,例如:GetFullPath、GetDirectoryName ...等等。

回答by frusty

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

使用 SafeFileName 而不是 FileName,它将返回一个没有路径的名称(和扩展名)。

回答by Rzgar

Use this code to put the filename in PictureNameTextEdit:

使用此代码将文件名放入 PictureNameTextEdit:

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

回答by Rzgar

//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);