使用 OpenFileDialog 属性“FileName”时仅获取文件名
我试图仅包含我在 label1.Text 属性的 OpenFileDialog 中选择的文件的文件名,但我还没有找到解决方案。 我知道我可以使用 ofd 实例上的 string 类中的方法来过滤掉文件的整个路径,但我想知道是否存在更智能/更快的方法?
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 +"";
}
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 +"";
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 OpenFileDialog.SafeFileName
OpenFileDialog.SafeFileName
获取对话框中所选文件的文件名和扩展名。文件名不包含路径。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.使用: Path.GetFileName 方法
Use: Path.GetFileName Method