文件名"用打开文件对话框财产和QUOT时只取得文件名; [英] Obtaining only the filename when using OpenFileDialog property "FileName"

查看:120
本文介绍了文件名"用打开文件对话框财产和QUOT时只取得文件名;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想包括我在打开文件对话框中label1.Text属性选择的,只有文件的文件名,但我还没有找到一个解决办法。 我知道我可以使用的方法从字符串类的OFD实例的完整路径过滤掉的文件,但我想知道,如果一个更聪明/更快的方法存在?

 打开文件对话框OFD =新的OpenFileDialog();
ofd.Title =查找歌曲;
ofd.Filter =MP3文件| * .MP3;
ofd.InitialDirectory = @C:\;
如果(ofd.ShowDialog()== DialogResult.OK)
{
   label1.Text =+ ofd.FileName +;
}
 

解决方案

使用<一个href="http://msdn.microsoft.com/en-us/library/system.windows.forms.openfiledialog.safefilename.aspx">OpenFileDialog.SafeFileName

OpenFileDialog.SafeFileName 获取文件名和扩展名在对话框中选择的文件。文件名不包括路径

I am trying to include only the filename of the file I've selected in the OpenFileDialog in the label1.Text property, but I haven't found a solution yet. I know I could use a method from the string class on the ofd instance to filter out the whole path to the file, but I would like to know if a smarter/quicker way exists?

OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "Find song";
ofd.Filter = "MP3 files|*.mp3";
ofd.InitialDirectory = @"C:\";
if (ofd.ShowDialog() == DialogResult.OK)
{
   label1.Text = "" + ofd.FileName +"";
}

解决方案

Use OpenFileDialog.SafeFileName

OpenFileDialog.SafeFileName Gets the file name and extension for the file selected in the dialog box. The file name does not include the path.

这篇关于文件名&QUOT;用打开文件对话框财产和QUOT时只取得文件名;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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