如何从保存文件对话框只检索文件名 [英] How retrieve only filename from save file dialog
问题描述
我有一个保存文件对话框,我想只得到文件名输入。相当于
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屋!