惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
P
Proofpoint News Feed
宝玉的分享
宝玉的分享
人人都是产品经理
人人都是产品经理
博客园_首页
爱范儿
爱范儿
博客园 - 叶小钗
aimingoo的专栏
aimingoo的专栏
S
SegmentFault 最新的问题
MyScale Blog
MyScale Blog
阮一峰的网络日志
阮一峰的网络日志
IT之家
IT之家
Microsoft Security Blog
Microsoft Security Blog
Blog — PlanetScale
Blog — PlanetScale
博客园 - 【当耐特】
Y
Y Combinator Blog
量子位
博客园 - 三生石上(FineUI控件)
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
The Blog of Author Tim Ferriss
月光博客
月光博客
有赞技术团队
有赞技术团队
Apple Machine Learning Research
Apple Machine Learning Research
A
About on SuperTechFans

博客园 - Dragon-China

JS实现显示倒影的图片展示特效之改变图片显示大小 C#制作WinForm控件 InfoPath: Passing Command Line parameters to a new form Office 2003 主 Interop 程序集的安装和使用 .NET 反编译工具及其插件 Extensible OLE Property Pages in .NET COM组件设计与应用之GUID和接口 用C#创建COM对象 ActiveX and Com Infopath中,自己编写的ActiveX控件,为何不能绑定数据到XML属性节点 一位软件工程师的6年总结本人读了深受启发,献给所有从事IT开发的人[转贴] InfoPath 的自动化和扩展 创建可绑定到 InfoPath 表单数据的 ActiveX 控件 Extending the InfoPath control set with custom controls InfoPath2007 开发人员的新增功能 在Infopath中使用自定义控件不能激发 OnPropertyChange 事件 ActiveX控件和它的容器 值得推荐的Infopath开发例子和资料 Custom Control support in InfoPath
转:如何用C#获得文件信息以及扩展信息
Dragon-China · 2008-04-24 · via 博客园 - Dragon-China

转:http://blog.csdn.net/knight94/archive/2006/05/07/711327.aspx

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;


namespace ExcelToInfopath
{
    
/// <summary>
    
/// CFileInfo 的摘要说明。
    
/// </summary>

    public class CFileInfo
    
{
        
private string sFileName ="";
        
private string sFullFileName="";
        
private string sFileExtension="";
        
private string sFilePath = "";
        
private string sFileComment = "";
        
private string sFileAuthor = "";
        
private string sFileTitle = "";
        
private string sFileSubject = "";
        
private string sFileCategory = "";
        
private string sFileType = "";
        
private long lFileLength = 0;
        
private long lFileVersion = 0;
        
private DateTime dCreationDate;
        
private DateTime 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;               

                
"read File Details"
            }

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

        }

 

        
"属性" 

        
"方法"
    }


 

    
// 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;
        }

    }

}

我是只取JPG图片的备注信息,所以:

  ShellClass sh = new ShellClass();
   Folder dir = sh.NameSpace(Path.GetDirectoryName(PictureFilepath));   
   FolderItem item = dir.ParseName( Path.GetFileName(PictureFilepath));
  string fileRemark = 
dir.GetDetailsOf(item,14);