如何仅从OpenFileDialog获取文件名和扩展名 [英] How do I get File Name and Extension only from OpenFileDialog

查看:142
本文介绍了如何仅从OpenFileDialog获取文件名和扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好(祝大家新年快乐)



我使用一段基本代码来选择一个文件,但我只能得到满满的路径(我需要)但似乎无法找到提取文件名和扩展名的方法

Hello everyone (and Happy New Year to all)

I am using a basic piece of code to select a file but I am only able to get the full path (which I need) but can't seem to find a way to extract the file name and extension only

Dim ofd As New OpenFileDialog
Dim dir As String

dir = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)

ofd.Filter = "All Files(*.*)|*.*|All files (*.*)|*.*"

ofd.FilterIndex = 2
ofd.InitialDirectory = dir

If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
     MsgBox(ofd.FileName)
End If

< br $>


MsgBox显示文件名和扩展名的完整路径。



你能告诉我怎么样吗只获取文件名和扩展名。



非常感谢



The MsgBox shows the full path with file name and extension.

Could you please advise how I can get only the file name and extension.

Many thanks

推荐答案

尝试:
If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
     MsgBox(Path.GetFileName(ofd.FileName))
End If


请访问以下链接:



从c-sharp的路径字符串中获取文件名 [ ^ ]

获取文件名 [ ^ ]



Please visit the following links:

Get file name from a path string in c-sharp[^]
Get the file name[^]

Dim ofd As New OpenFileDialog
  Dim dir As String

  dir = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)

  ofd.Filter = "All Files(*.*)|*.*|All files (*.*)|*.*"

  ofd.FilterIndex = 2
  ofd.InitialDirectory = dir

  If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
       MsgBox(Path.GetFileName(ofd.FileName))
  End If


这篇关于如何仅从OpenFileDialog获取文件名和扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆