dows 2000, Windows Server 2003 and Windows XP must use StgCreateStorageEx rather than StgCreateDocfile to take advantage of the enhanced Windows 2000 and Windows XP Structured Storage features.
*
*/
using System;
using System.Collections.Generic;
using System.Text;
using StructuredStorageWrapper;
namespace WindowsApplication8
{
class FileSummary
{
public static void SetProperty(string filename,string msg , SummaryPropId summaryType)
{
// first you need to either create or open a file and its
// property set stream
//申明接口(指针)
IPropertySetStorage propSetStorage = null;
//com 组件的 clsid 参见IPropertySetStorage定义
Guid IID_PropertySetStorage = new
Guid("0000013A-0000-0000-C000-000000000046");
//Applications written for Windows 2000, Windows Server 2003 and Windows XP must use StgCreateStorageEx rather than StgCreateDocfile to take advantage of the enhanced Windows 2000 and Windows XP Structured Storage features
uint hresult = ole32.StgOpenStorageEx(
filename,
(int)(STGM.SHARE_EXCLUSIVE | STGM.READWRITE),
(int)STGFMT.FILE,
0,
(IntPtr)0,
(IntPtr)0,
ref IID_PropertySetStorage,
ref propSetStorage); //返回指针
// next you need to create or open the Summary Information property set
Guid fmtid_SummaryProperties = new
Guid("F29F85E0-4FF9-1068-AB91-08002B27B3D9");
IPropertyStorage propStorage = null;
hresult = propSetStorage.Create(
ref fmtid_SummaryProperties,
(IntPtr)0,
(int)PROPSETFLAG.DEFAULT,
(int)(STGM.CREATE | STGM.READWRITE |
STGM.SHARE_EXCLUSIVE),
ref propStorage);
// next, you assemble a property descriptor for the property you
// want to write to, in our case the Comment property
PropSpec propertySpecification = new PropSpec();
propertySpecification.ulKind = 1;
propertySpecification.Name_Or_ID = new
IntPtr((int)summaryType);
//now, set the value you want in a property variant
PropVariant propertyValue = new PropVariant();
propertyValue.FromObject(msg);
// Simply pass the property spec and its new value to the WriteMultiple
// method and you're almost done
propStorage.WriteMultiple(1, ref propertySpecification, ref
propertyValue, 2);
// the only thing left to do is commit your changes. Now you're done!
hresult = propStorage.Commit((int)STGC.DEFAULT);
//下面的很关键,如何关闭一个非托管的指针,如果不关闭,则本程序不关闭,文件被锁定!
System.Runtime.InteropServices.Marshal.ReleaseComObject(propSetStorage);
propSetStorage = null;
GC.Collect();
}
}
}
把ole32一些方法进行COM封装
using System;
using System.Text;
using System.Runtime.InteropServices;
namespace StructuredStorageWrapper
{
public enum SummaryPropId : int
{
Title = 0x00000002,
Subject = 0x00000003,
Author = 0x00000004,
Keywords = 0x00000005,
Comments = 0x00000006,
Template = 0x00000007,
LastSavedBy = 0x00000008,
RevisionNumber = 0x00000009,
TotalEditingTime = 0x0000000A,
LastPrinted = 0x0000000B,
CreateDateTime = 0x0000000C,
LastSaveDateTime = 0x0000000D,
NumPages = 0x0000000E,
NumWords = 0x0000000F,
NumChars = 0x00000010,
Thumbnail = 0x00000011,
AppName = 0x00000012,
Security = 0x00000013
}
public enum STGC : int
{
DEFAULT = 0,
OVERWRITE = 1,
ONLYIFCURRENT = 2,
DANGEROUSLYCOMMITMERELYTODISKCACHE = 4,
CONSOLIDATE = 8
}
public enum PROPSETFL |