如何从保存文件对话框只检索文件名 [英] How retrieve only filename from save file dialog

查看:166
本文介绍了如何从保存文件对话框只检索文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个保存文件对话框,我想只得到文件名输入。相当于

I have a save file dialog and i want to get only the filename entered. Equivalent for

    openfiledialog.SafeFileName;

保存文件对话框没有 SafeFileName 属性和文件名返回两个文件名,路径和扩展。请我如何提取仅文件名。

Save file dialog has no SafeFileName Property and FileName returns both filename, path and extension. Pls how do i extract only file name.

推荐答案

如果你想扩展用路径文件名。用GetFileName()。如果你想让它不带扩展名,以及用 Path.GetFileNameWithoutExtension()

If you want the filename WITH extension use Path.GetFileName(). If you want it WITHOUT the extension as well use Path.GetFileNameWithoutExtension().

public void Test(string fileName)
{
    string path = Path.GetDirectoryName(fileName);
    string filename_with_ext = Path.GetFileName(fileName);
    string filename_without_ext = Path.GetFileNameWithoutExtension(fileName);
    string ext_only = Path.GetExtension(fileName);
}

有关详细信息,请参阅MSDN,尤其是路径类具有许多有用的方法:

See MSDN for further details, especially the Path class which has a number of useful methods:

http://msdn.microsoft.com/en-us/library/System.IO.Path_methods.aspx

http://msdn.microsoft.com/en-us/library/ system.io.path.getfilename.aspx

http://msdn.microsoft.com/en-us/library/system.io.path.getfilenamewithoutextension.aspx

这篇关于如何从保存文件对话框只检索文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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