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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 11:01:21  来源:igfitidea点击:

How to get file extension from OpenFileDialog?

c#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

As stated in here, you can do something like this: Path.GetExtension(photoFile.FileName)

如规定在这里,你可以这样做:Path.GetExtension(photoFile.FileName)

回答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();