C# 如何从 OpenFileDialog 获取文件扩展名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9871811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get file extension from OpenFileDialog?
提问by M.Azad
I want just get Image(.JPG,.PNG,.Gif) File from my OpenFileDialogHow can I get file extension from OpenFileDialog?
我只想从我的
如何获取文件扩展名中获取 Image( .JPG, .PNG, .Gif) 文件?OpenFileDialogOpenFileDialog
Is it impossible?
不可能吗?
采纳答案by Steve
To filter only certain types of file use Filter Property
要仅过滤某些类型的文件,请使用过滤器属性
OpenFileDialog1.Filter = "Image Files (JPG,PNG,GIF)|*.JPG;*.PNG;*.GIF";
To get the file extension use the Path helper GetFileExtension
要获取文件扩展名,请使用 Path helper GetFileExtension
if (OpenFileDialog1.ShowDialog() == DialogResult.OK)
string ext = Path.GetExtension(OpenFileDialog1.FileName);
回答by Aliostad
What about
关于什么
Path.GetExtension(ofd.FileName);
回答by ionden
Use this:
用这个:
Path.GetExtension(dialog.FileName);
回答by npinti
回答by Manu
Try this
尝试这个
fileDialog.File.Extension
回答by Ali
Also could use Extension Method as blow:
也可以使用扩展方法作为打击:
public static class Helper
{
public static string GetFileExtention(this OpenFileDialog dialog)
{
return Path.GetExtension(dialog.FileName);
}
}
And simply use it by:
只需通过以下方式使用它:
openFileDialog1.ShowDialog();
string foo = openFileDialog1.GetFileExtention();

