仅在使用 OpenFileDialog 时返回 FileName [英] Return FileName Only when using OpenFileDialog
本文介绍了仅在使用 OpenFileDialog 时返回 FileName的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用以下方法浏览文件:
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?
FileName 方法返回完整的路径和文件名.
The FileName method returns the entire path and file name.
即我想要 Foo.txt 而不是 C:\SomeDirectory\Foo.txt
i.e. I want Foo.txt instead of C:\SomeDirectory\Foo.txt
推荐答案
使用 Path.GetFileName(fullPath)
只获取文件名部分,如下所示:
Use Path.GetFileName(fullPath)
to get just the filename part, like this:
OpenFileDialog.ShowDialog()
PictureNameTextEdit.Text = System.IO.Path.GetFileName(OpenFileDialog.FileName)
这篇关于仅在使用 OpenFileDialog 时返回 FileName的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文