用户操作
[即时聊天] [发私信] [加为好友]
曾洪宁ID:Knight94
279153次访问,排名223,好友0人,关注者51人。
Knight94的文章
原创 86 篇
翻译 0 篇
转载 1 篇
评论 620 篇
Knight94的公告
抉择?!
邮箱为 knight94cn@21cn.com
最近评论
lanyur:后面的怎么不写了呢?
bmoon:愚翁老师,非常感谢您的知识,您的blog一直在引导我的成长,谢谢
bmoon:愚翁老师,非常感谢您的知识,您的blog一直在引导我的成长,谢谢
hongxuyao:受益匪浅,严重支持!!!
levenwood:感激涕凌(最后一字错了)
文章分类
收藏
    相册
    网址推荐
    Code Project
    FAQ Asp.Net
    FAQ WinForm
    存档
    软件项目交易
    订阅我的博客
    XML聚合  FeedSky
    订阅到鲜果
    订阅到Google
    订阅到抓虾
    订阅到BlogLines
    订阅到Yahoo
    订阅到GouGou
    订阅到飞鸽
    订阅到Rojo
    订阅到newsgator
    订阅到netvibes

    原创 如何用C#获得文件信息以及扩展信息收藏

    新一篇: 浅谈八皇后问题 | 旧一篇: 如何解决WebService参数传递中文的问题

    C#中获得文件信息很容易,只需要用FileInfo类或者FileVersionInfo类就可以获得,但是如果想要获得文件的扩展信息,则无法从这两类来获得。不过在C#中,这也不是件难事,只要引入“Microsoft Shell Controls and Automation”这个COM就可以获得。

     

    接下来就分别来介绍。

    首先介绍FileInfo类,这个类非常简单,首先需要根据文件名来创建FileInfo对象,例如:

    using System.IO;

    FileInfo fi = new FileInfo( yourFileName );

     

    那么以后就可以通过此对象来访问文件一些属性,例如文件大小,创建时间,最后访问时间,最后写入时间等等,还可以通过访问对象的Attributes属性,来获得当前文件是只读、隐藏之类属性,这里我就不细说了,详情参看MSDN

     

    第二个要说的,就是FileSystemInfo类,这个类是FileInfo类的基类,这里也就不多说了。

     

    第三个要说的,就是如何判断一个文件的Version信息,这就需要来介绍FileVersionInfo这个类。但是并不是所有的文件都有Version信息,因此在使用FileVersionInfo的时候需要注意的是,最好先判断一下文件的扩展名。不过一个FileVersionInfo类对象不能通过构造函数来创建,需要调用类的静态方法来获得,例如:

    using System.Diagnostics;

    FileVersionInfo fvi = FileVersionInfo.GetVersionInfo( yourFileName );

     

    通过此对象,可以获得文件的产品名称,公司名,版本号,语言版本,版权等等,这方面详情可以参看MSDN

     

    最后要说的,就是如何得到一个文件的扩展信息,例如标题,作者等等,这些信息从如上三个类中是无法获得。但是要在C#程序中获得,就需要引入一个“Microsoft Shell Controls and Automation”这个COM,这个COM是由系统“Shell32.dll”而提供。这方面的例子,可以参看如下这篇文章。

    http://www.codeproject.com/cs/files/detailedfileinfo.asp

     

    为了方便大家使用,我把其中类的代码贴出来。

    using Shell32; // Use this namespace after add the reference

        /// <summary>

        /// Returns the detailed Information of a given file.

        /// </summary>

        public class CFileInfo

        {

     

            private string sFileName ="",

                sFullFileName="",

                sFileExtension="",

                sFilePath = "",

                sFileComment = "",

                sFileAuthor = "",

                sFileTitle = "",

                sFileSubject = "",

                sFileCategory = "",

                sFileType = "";

     

            private long lFileLength = 0,

                lFileVersion = 0;

     

            private DateTime dCreationDate,

                dModificationDate;

     

            public CFileInfo(string sFPath)

            {

               

                // check if the given File exists

                if(File.Exists(sFPath))

                {

                    ArrayList aDetailedInfo = new ArrayList();

     

                    FileInfo oFInfo = new FileInfo(sFPath);

     

                    sFileName = oFInfo.Name;

                    sFullFileName = oFInfo.FullName;

                    sFileExtension = oFInfo.Extension;

                    lFileLength=oFInfo.Length;

                    sFilePath = oFInfo.Directory.ToString();

                    dCreationDate = oFInfo.CreationTime;

                    dModificationDate = oFInfo.LastWriteTime;

                   

                    #region "read File Details"

                   

                    aDetailedInfo = GetDetailedFileInfo(sFPath);

     

                    foreach(DetailedFileInfo oDFI in aDetailedInfo)

                    {

                        switch(oDFI.ID)

                        {

                            case 2:

                                sFileType = oDFI.Value;

                                break;

                            case 9:

                                sFileAuthor = oDFI.Value;

                                break;

                            case 10:

                                sFileTitle = oDFI.Value;

                                break;

                            case 11:

                                sFileSubject = oDFI.Value;

                                break;

                            case 12:

                                sFileCategory = oDFI.Value;

                                break;

                            case 14:

                                sFileComment = oDFI.Value;

                                break;

                            default:

                                break;

                        }

     

                    }

                    #endregion

                }

                else

                {

                    throw new Exception("The given File does not exist");

                }

     

            }

     

     

            #region "Properties"

            public string FileName

            {

                get{return sFileName;}

                set{sFileName=value;}

            }

     

            public string FilePath

            {

                get{return sFilePath;}

                set{sFilePath = value;}

            }

     

            public string FullFileName

            {

                get{return sFullFileName;}

                set{sFullFileName=value;}

            }

     

            public string FileExtension

            {

                get{return sFileExtension;}

                set{sFileExtension=value;}

            }

     

       

            public long FileSize

            {

                get{return lFileLength;}

                set{lFileLength=value;}

            }

     

            public long FileVersion

            {

                get{return lFileVersion;}

                set{lFileVersion=value;}

            }

     

            public DateTime FileCreationDate

            {

                get{return dCreationDate;}

                set{dCreationDate=value;}

            }

     

            public DateTime FileModificationDate

            {

                get{return dModificationDate;}

                set{dModificationDate=value;}

            }

           

            public string FileType

            {

                get{return sFileType;}

            }

     

     

            public string FileTitle

            {

                get{return sFileTitle;}

            }

     

            public string FileSubject

            {

                get{return sFileSubject ;}

            }

     

            public string FileAuthor

            {

                get{return sFileAuthor ;}

            }

     

            public string FileCategory

            {

                get{return sFileCategory ;}

            }

     

            public string FileComment

            {

                get{return sFileComment ;}

            }

     

     

            #endregion

     

            #region "Methods"

            private ArrayList GetDetailedFileInfo(string sFile)

            {

                ArrayList aReturn = new ArrayList();

                if(sFile.Length>0)

                {

                    try

                    {

     

                        // Creating a ShellClass Object from the Shell32

                        ShellClass sh = new ShellClass();

                        // Creating a Folder Object from Folder that inculdes the File

                        Folder dir = sh.NameSpace( Path.GetDirectoryName( sFile ) );

                        // Creating a new FolderItem from Folder that includes the File

                        FolderItem item = dir.ParseName( Path.GetFileName( sFile ) );

                        // loop throw the Folder Items

                        for( int i = 0; i < 30; i++ )

                        {

                            // read the current detail Info from the FolderItem Object

                            //(Retrieves details about an item in a folder. For example, its size, type, or the time of its last modification.)

                            // some examples:

                            // 0 Retrieves the name of the item.

                            // 1 Retrieves the size of the item.

                            // 2 Retrieves the type of the item.

                            // 3 Retrieves the date and time that the item was last modified.

                            // 4 Retrieves the attributes of the item.

                            // -1 Retrieves the info tip information for the item.

     

                            string det = dir.GetDetailsOf( item, i );

                            // Create a helper Object for holding the current Information

                            // an put it into a ArrayList

                            DetailedFileInfo oFileInfo = new DetailedFileInfo(i,det);

                            aReturn.Add(oFileInfo);

                        }

     

                    }

                    catch(Exception)

                    {

     

                    }

                }

                return aReturn;

            }

            #endregion

        }

     

        // Helper Class from holding the detailed File Informations

        // of the System

        public class DetailedFileInfo

        {

            int iID = 0;

            string sValue ="";

     

            public int ID

            {

                get{return iID;}

                set

                {

                    iID=value;

                }

            }

            public string Value

            {

                get{return sValue;}

                set{sValue = value;}

            }

     

            public DetailedFileInfo(int ID, string Value)

            {

                iID = ID;

                sValue = Value;

            }

        }

     

    发表于 @ 2006年05月07日 09:00:00|评论(loading...)|编辑

    新一篇: 浅谈八皇后问题 | 旧一篇: 如何解决WebService参数传递中文的问题

    评论

    #ghost100 发表于2006-05-16 08:57:00  IP: 220.165.206.*
    感谢愚翁,需要的正是这个东东。谢谢。
    #feifei 发表于2006-06-14 18:20:00  IP: 220.160.73.*
    有谁知道FileSystemInfo的效率如何,如果文件数量非常大的话?
    #狼之舞 发表于2006-08-14 13:51:00  IP: 218.6.199.*
    非常感谢,正在查找此东东
    #狼之舞 发表于2006-10-13 12:06:00  IP: 218.6.199.*
    能不能在程序中动态修改此类信息????
    #User 发表于2006-10-13 14:34:00  IP: 219.238.164.*
    请教一个数据库问题:
    kguid主键,数据格式:abc.123.bbb
    split为自定义函数,实现能取到123
    查询
    select * from tb where split(kguid, 1, '.')='abc'
    这样是否失去了kguid索引的价值,影响查询效率
    #knight94 发表于2006-10-13 14:54:00  IP: 218.71.239.*
    to User

    为什么不这样进行查询呢
    select * from tb where kguid like 'abc.%'

    不过这样效率虽说比你的要好一些,但是like语句还是比较慢的。
    #knight94 发表于2006-10-13 14:55:00  IP: 218.71.239.*
    to 狼之舞

    有些属性不太容易修改。

    你最好能具体说说,打算修改些什么属性
    #jinseyd 发表于2006-12-13 11:21:27  IP: 203.85.42.*
    To:愚翁 非常感谢你,这种方法可以获取文件的摘要。 但是有些文件(eg:excel,word...)属性里面有自定,那些信息怎么获取?
    #iamxp 发表于2007-02-14 09:22:05  IP: 61.114.254.*
    如果想动态修改摘要中的"作者"信息,是否可以实现?另,这段代码是否支持非office的文档?我测试过一个DOSFile组件,不支持对非office文件写入摘要信息,请问有其它方法可以实现吗?
    #wakilili 发表于2008-06-14 12:07:56  IP: 203.86.25.*
    可以修改摘要中的"作者"信息吗???
    #yaji166 发表于2008-06-17 02:27:54  IP: 218.59.99.*
    好东东 感谢楼主 天皇替我在日本给你磕头了
    发表评论  


    当前用户设置只有注册用户才能发表评论。如果你没有登录,请点击登录
    Csdn Blog version 3.1a
    Copyright © Knight94