为回答一个CSDN上的问题Google了半天,居然没能找到一个像样的C#访问Structure Storage的例子。没办法,自己动手丰衣足食了。:)
用C#作Structure Storage最大的麻烦是无数的COM Interface和API调用,实在没有心情把一堆COM接口函数都写出来了,只写了用到的几个: StgOpenStorageEx、IPropertySetStorage::Open、IPropertyStorage::ReadMultiple,而且功能也不全,只是取得Document的Creating Application属性,但是可以解决上面的问题了。(下载,没有找到合适的上传空间,随手申请了一个Free的)
如何判断一个文档是否是office文档????
FROM MY REPLY:
Office文件都是使用结构化存储复合文件(Structured Storage, Compound File),可以从它们的文件属性里面取得创建的应用程序,例如,在文件属性对话框当中的Summary一页有定义Application Name,可以看到文件使用Excel或者Word创建的。
但是在dotNet程序读取这些信息比较麻烦:
1)需要声明StgOpenStorageEx函数,包括参数的结构和常量定义。
2) 需要声明IPropertySetStorage,IPropertyStorage和IEnumSTATPROPSTG COM接口。
3) 定义Summary Information property set的FMTID:F29F85E0-4FF9-1068-AB91-08002B27B3D9。
4)通过StgOpenStorageEx打开文件,取得IPropertySetStorage.
5)用ReadMultiple读取Property ID = 0x12 (Creating Application),如果名字是Excel或者Word,那么是Office文件。
打印 | 张贴于 2004-08-04 11:44:00 | Tag:.NET
留言反馈
miaojunzhe@yahoo.com
Thanks!
yfsoft7937@hotmail.com
Thanks
下载URL已不可用,哪位好心人能给我发一份吗?
MSN:abyss7i[at]hotmail.com
或者:abyss7i@gmail.com
读取的代码如下:
public string GetProperty(SummaryPropId summaryPropertID)
{
// Open the file and its property set stream
IPropertySetStorage propSetStorage = null;
Guid IID_PropertySetStorage = new
Guid("0000013A-0000-0000-C000-000000000046");
uint hresult = ole32.StgOpenStorageEx(@"c:\z.txt",
(int)(STGM.READ | STGM.SHARE_DENY_WRITE),
(int)STGFMT.ANY,
0,
System.IntPtr.Zero,
System.IntPtr.Zero,
ref IID_PropertySetStorage,
ref propSetStorage);
// Open the Summary Information property set
Guid fmtid_SummaryProperties = new
Guid("F29F85E0-4FF9-1068-AB91-08002B27B3D9");
IPropertyStorage propStorage = null;
int hresult2 = propSetStorage.Open(
ref fmtid_SummaryProperties,
(int)(STGM.READ | STGM.SHARE_EXCLUSIVE),
out propStorage);
// Specify the property to be retrieved
PropSpec[] propSpecs = new PropSpec[1];
propSpecs[0].ulKind = 1;
propSpecs[0].Name_Or_ID = new IntPtr((int)summaryPropertID);
PropVariant[] propertyValues = new PropVariant[1];
// Retrieve the value
hresult2 = propStorage.ReadMultiple(1, propSpecs, propertyValues);
// Release the Com objects
Marshal.ReleaseComObject(propStorage);
Marshal.ReleaseComObject(propSetStorage);
propStorage = null;
propSetStorage = null;
return Marshal.PtrToStringUni(propertyValues[0].pointerValue);
}
create IPropertySetStorage failed! File is not an Office document.
为什么啊?
在应用程序里支持VBA,office操作部分用VBA来写比较容易一点……
刚刚在为这个问题而困扰.....