plz帮助其检查文件扩展名的功能 [英] plz help its a function to check file extension

查看:94
本文介绍了plz帮助其检查文件扩展名的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

byte[] ReadFile(string sPath)
        {
            //Initialize byte array with a null value initially.
            byte[] data = null;

            //Use FileInfo object to get file size.
            FileInfo fInfo = new FileInfo(sPath);
            try
            {
                if(fInfo.Extension == "jpg")
                {
                    long numBytes = fInfo.Length;

                    //Open FileStream to read file
                    FileStream fStream = new FileStream(sPath, FileMode.Open, FileAccess.Read);

                    //Use BinaryReader to read file stream into byte array.
                    BinaryReader br = new BinaryReader(fStream);

                    //When you use BinaryReader, you need to supply number of bytes to read from file.
                    //In this case we want to read entire file. So supplying total number of bytes.
                    data = br.ReadBytes((int)numBytes);
                    return data;
                }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }

推荐答案

您还没有问过任何问题.
我仍然建议使用Path.GetExtension来获取扩展名
http://msdn.microsoft.com/en-us/library/system. io.path.getextension.aspx [ ^ ]
You haven''t asked any question.
Still I would suggest to use Path.GetExtension to get the extension
http://msdn.microsoft.com/en-us/library/system.io.path.getextension.aspx[^]


这篇关于plz帮助其检查文件扩展名的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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