知秋一叶

日出而作,日入而息,凿井而饮,耕田而食,帝力何有于我哉
随笔 - 55, 评论 - 311, 引用 - 79

导航

工具

标签

每月存档

广告



访客

 

  为回答一个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文件。

相关文章

Loading...

打印 | 张贴于 2004-08-04 11:44:00 | Tag:.NET

留言反馈

#re: Structured Storage 编辑
could you send me the c# codes too?

miaojunzhe@yahoo.com

Thanks!
2006-05-27 05:32:00 | [匿名用户:miao]
#re: Structured Storage 编辑
Can you send me your C# code?

yfsoft7937@hotmail.com

Thanks

2005-12-09 17:23:00 | [匿名用户:flyingbugs]
#re: Structured Storage 编辑
我现在碰到个问题,可以用WriteMultiple()写入数据,但用ReadMultiple()读不了,返回的结果都是0。

下载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);

}
2005-11-11 16:16:00 | [匿名用户:abyss7i]
#re:Structured Storage 编辑
Structured Storageooeess
2005-06-14 10:58:00 | [匿名用户:一氧化碳测定仪]
#re:Structured Storage 编辑
^_~,pretty good!csharpsseeoo
2005-05-16 17:59:00 | [匿名用户:PID调节器]
#佩服 编辑
佩服
2005-04-22 11:01:00 | [匿名用户:新鲜鱼排]
#re:Structured Storage 编辑
^_^,Pretty Good!
2005-04-16 01:45:00 | [匿名用户:邵氏硬度计]
#re:Structured Storage 编辑
^_^,Pretty Good!
2005-04-10 19:56:00 | [匿名用户:湿度测控仪]
#re: Structured Storage 编辑
每次都返回如下信息:
create IPropertySetStorage failed! File is not an Office document.

为什么啊?
2005-01-18 16:51:00 | [匿名用户:sunbow]
#re: Structured Storage 编辑
同意qqchen的意见,该如何解决呢?
2004-09-14 10:09:00 | [匿名用户:tonyking]
#re: Structured Storage 编辑
office automation在Client程序用当然很好,但是如果是Server程序(比如说Indexing Service),这个太“重“了。
2004-08-06 05:55:00 | [匿名用户:qqchen]
#re: Structured Storage 编辑
同jeric……
在应用程序里支持VBA,office操作部分用VBA来写比较容易一点……
2004-08-06 04:51:00 | [匿名用户:jiangsheng]
#re: Structured Storage 编辑
如何在得出当前dotnet运行的服务器的IIS版本?

刚刚在为这个问题而困扰.....
2004-08-05 13:31:00 | [匿名用户:lion]
#re: Structured Storage 编辑
C# 操作Office Automation已经很爽了,前一段时间用C++操作office automation简直要吐血。
2004-08-04 14:07:00 | [匿名用户:jeric]
#re: Structured Storage 编辑
你真是太伟大了,经常在CSDN上看到你的帖子,向你学习!
2004-08-04 13:08:00 | [匿名用户:Alex]
对不起,目前本随笔不允许发表新评论.

Powered by: Joycode MVC Blogger System