Windows Delphi:如何设置(写入)扩展文件属性?

Windows Delphi:如何设置(写入)扩展文件属性?,windows,delphi,file-properties,Windows,Delphi,File Properties,我想设置Windows资源管理器文件属性->摘要选项卡(作者、标题、主题等)中显示的属性。(Windows 7中的“详细信息”选项卡) 我已经知道如何让他们使用 目标文件扩展名是xls、xlsx、csv、txt和jpg文件。 操作系统:Windows2003/2008/XP/Windows7。 请注意,这段代码将在web服务应用程序中运行,而服务器没有安装Excel 注意:似乎没有关于如何设置它们的信息/示例代码 使用方法编写摘要信息。参见MSDN示例。不是delphi,但很容易转换。函数Get

我想设置Windows资源管理器文件属性->摘要选项卡(作者、标题、主题等)中显示的属性。(Windows 7中的“详细信息”选项卡) 我已经知道如何让他们使用

目标文件扩展名是xls、xlsx、csv、txt和jpg文件。 操作系统:Windows2003/2008/XP/Windows7。 请注意,这段代码将在web服务应用程序中运行,而服务器没有安装Excel


注意:似乎没有关于如何设置它们的信息/示例代码

使用方法编写摘要信息。参见MSDN示例。不是delphi,但很容易转换。

函数GetFileSummaryInfo(const FileName:WideString):String;
function GetFileSummaryInfo(const FileName: WideString): String;

const
FmtID_SummaryInformation: TGUID =     '{F29F85E0-4FF9-1068-AB91-08002B27B3D9}';
FMTID_DocSummaryInformation : TGUID = '{D5CDD502-2E9C-101B-9397-08002B2CF9AE}';
FMTID_UserDefinedProperties : TGUID = '{D5CDD505-2E9C-101B-9397-08002B2CF9AE}';
IID_IPropertySetStorage : TGUID =     '{0000013A-0000-0000-C000-000000000046}';

const
STGFMT_FILE = 3; //Indicates that the file must not be a compound file.
                 //This element is only valid when using the StgCreateStorageEx
                 //or StgOpenStorageEx functions to access the NTFS file system
                 //implementation of the IPropertySetStorage interface.
                 //Therefore, these functions return an error if the riid
                 //parameter does not specify the IPropertySetStorage interface,
                 //or if the specified file is not located on an NTFS file system
                 //volume.

STGFMT_ANY = 4; //Indicates that the system will determine the file type and
                //use the appropriate structured storage or property set
                //implementation.
                //This value cannot be used with the StgCreateStorageEx function.


// Summary Information
 PID_TITLE        = 2;
 PID_SUBJECT      = 3;
 PID_AUTHOR       = 4;
 PID_KEYWORDS     = 5;
 PID_COMMENTS     = 6;
 PID_TEMPLATE     = 7;
 PID_LASTAUTHOR   = 8;
 PID_REVNUMBER    = 9;
 PID_EDITTIME     = 10;
 PID_LASTPRINTED  = 11;
 PID_CREATE_DTM   = 12;
 PID_LASTSAVE_DTM = 13;
 PID_PAGECOUNT    = 14;
 PID_WORDCOUNT    = 15;
 PID_CHARCOUNT    = 16;
 PID_THUMBNAIL    = 17;
 PID_APPNAME      = 18;
 PID_SECURITY     = 19;

 // Document Summary Information
 PID_CATEGORY     = 2;
 PID_PRESFORMAT   = 3;
 PID_BYTECOUNT    = 4;
 PID_LINECOUNT    = 5;
 PID_PARCOUNT     = 6;
 PID_SLIDECOUNT   = 7;
 PID_NOTECOUNT    = 8;
 PID_HIDDENCOUNT  = 9;
 PID_MMCLIPCOUNT  = 10;
 PID_SCALE        = 11;
 PID_HEADINGPAIR  = 12;
 PID_DOCPARTS     = 13;
 PID_MANAGER      = 14;
 PID_COMPANY      = 15;
 PID_LINKSDIRTY   = 16;
 PID_CHARCOUNT2   = 17;

var
 I: Integer;
 PropSetStg: IPropertySetStorage;
 PropSpec: array of TPropSpec;
 PropStg: IPropertyStorage;
 PropVariant: array of TPropVariant;
 Rslt: HResult;
 S: String;
 Stg: IStorage;
 PropEnum: IEnumSTATPROPSTG;
 HR : HResult;
 PropStat: STATPROPSTG;
 k : integer;

function PropertyPIDToCaption(const ePID: Cardinal): string;
begin
 case ePID of
   PID_TITLE:
     Result := 'Title';
   PID_SUBJECT:
     Result := 'Subject';
   PID_AUTHOR:
     Result := 'Author';
   PID_KEYWORDS:
     Result := 'Keywords';
   PID_COMMENTS:
     Result := 'Comments';
   PID_TEMPLATE:
     Result := 'Template';
   PID_LASTAUTHOR:
     Result := 'Last Saved By';
   PID_REVNUMBER:
     Result := 'Revision Number';
   PID_EDITTIME:
     Result := 'Total Editing Time';
   PID_LASTPRINTED:
     Result := 'Last Printed';
   PID_CREATE_DTM:
     Result := 'Create Time/Date';
   PID_LASTSAVE_DTM:
     Result := 'Last Saved Time/Date';
   PID_PAGECOUNT:
     Result := 'Number of Pages';
   PID_WORDCOUNT:
     Result := 'Number of Words';
   PID_CHARCOUNT:
     Result := 'Number of Characters';
   PID_THUMBNAIL:
     Result := 'Thumbnail';
   PID_APPNAME:
     Result := 'Creating Application';
   PID_SECURITY:
     Result := 'Security';
   else
     Result := '$' + IntToHex(ePID, 8);
   end
end;

begin
 Result := '';
try
 OleCheck(StgOpenStorageEx(PWideChar(FileName),
 STGM_READ or STGM_SHARE_DENY_WRITE,
 STGFMT_FILE,
 0, nil,  nil, @IID_IPropertySetStorage, stg));

 PropSetStg := Stg as IPropertySetStorage;

 OleCheck(PropSetStg.Open(FmtID_SummaryInformation,
    STGM_READ or STGM_SHARE_EXCLUSIVE, PropStg));

 OleCheck(PropStg.Enum(PropEnum));
 I := 0;

 hr := PropEnum.Next(1, PropStat, nil);
  while hr = S_OK do
  begin
    inc(I);
    SetLength(PropSpec,I);
    PropSpec[i-1].ulKind := PRSPEC_PROPID;
    PropSpec[i-1].propid := PropStat.propid;
    hr := PropEnum.Next(1, PropStat, nil);
 end;

 SetLength(PropVariant,i);
 Rslt := PropStg.ReadMultiple(i, @PropSpec[0], @PropVariant[0]);

 if Rslt =  S_FALSE then Exit;

 for k := 0 to i -1 do
  begin
    S := '';
    if PropVariant[k].vt = VT_LPSTR then
      if Assigned(PropVariant[k].pszVal) then
       S := PropVariant[k].pszVal;

    S := Format(PropertyPIDToCaption(PropSpec[k].Propid)+ ' %s',[s]);
   if S <> '' then Result := Result + S + #13;
 end;
 finally
 end;
常数 FmtID_摘要信息:TGUID=“{F29F85E0-4FF9-1068-AB91-08002B27B3D9}”; FMTID_文档摘要信息:TGUID='{D5CDD502-2E9C-101B-9397-08002B2CF9AE}'; FMTID_UserDefinedProperties:TGUID='{D5CDD505-2E9C-101B-9397-08002B2CF9AE}'; IID_IPropertySetStorage:TGUID='{0000013A-0000-0000-C000-0000000000 46}'; 常数 STGFMT_文件=3//指示文件不能是复合文件。 //此元素仅在使用STGCreateStorageX时有效 //或stgopenstoragex函数来访问NTFS文件系统 //IPropertySetStorage接口的实现。 //因此,如果riid //参数未指定IPropertySetStorage接口, //或者指定的文件不在NTFS文件系统上 //音量。 STGFMT_ANY=4//指示系统将确定文件类型和 //使用适当的结构化存储或属性集 //实施。 //此值不能与STGCreateStorageX函数一起使用。 //摘要信息 PID_TITLE=2; PID_受试者=3; PID_作者=4; PID_关键字=5; PID_注释=6; PID_模板=7; PID_LASTAUTHOR=8; PID_REVNUMBER=9; PID_EDITTIME=10; PID_=11; PID_创建_DTM=12; PID_LASTSAVE_DTM=13; PID_页面计数=14; PID_字数=15; PID_CHARCOUNT=16; PID_=17; PID_APPNAME=18; PID_安全=19; //文件摘要信息 PID_类别=2; PID_格式=3; PID_字节数=4; PID_线数=5; PID_PARCOUNT=6; PID_SLIDECOUNT=7; PID_notecont=8; PID_HIDDENCOUNT=9; PID_MMCLIPCOUNT=10; PID_标度=11; PID_头对=12; PID_DOCPARTS=13; PID_管理器=14; PID_公司=15; PID_LINKSDIRTY=16; PID_CHARCOUNT2=17; 变量 I:整数; PropSetTG:IPropertySetStorage; PropSpec:TPropSpec的数组; 项目:IPropertyStorage; PropVariant:TPropVariant的数组; Rslt:HResult; S:字符串; Stg:历史; 属性:IEnumSTATPROPSTG; HR:HResult; PropStat:STATPROPSTG; k:整数; 函数propertypidtocation(const-ePID:Cardinal):字符串; 开始 流行性出血热1例 标题: 结果:=‘标题’; 主题: 结果:=‘受试者’; PID_作者: 结果:='作者'; PID_关键字: 结果:=‘关键字’; PID_评论: 结果:=‘评论’; PID_模板: 结果:='模板'; PID_最新作者: 结果:='上次保存人'; PID\U版本号: 结果:=‘修订号’; PID_编辑时间: 结果:='总编辑时间'; PID\u最后打印: 结果:='上次打印'; PID\u创建\u DTM: 结果:='创建时间/日期'; PID\u LASTSAVE\u DTM: 结果:='上次保存的时间/日期'; PID_页面计数: 结果:='页数'; PID_字数: 结果:=‘字数’; PID_字符数: 结果:='字符数'; PID_缩略图: 结果:=‘缩略图’; PID_应用程序名: 结果:='创建应用程序'; PID_安全: 结果:=‘安全’; 其他的 结果:='$'+IntToHex(ePID,8); 结束 结束; 开始 结果:=''; 尝试 OleCheck(stgopenstoragex(PWideChar(文件名)), STGM\u读取或STGM\u共享\u拒绝\u写入, STGFMT_文件, 0,无,无,@IID_IPropertySetStorage,stg); PropSetStg:=Stg作为IPropertySetStorage; OLECTHECK(PropSetStg.Open)(FmtID_摘要信息, STGM_READ或STGM_SHARE_EXCLUSIVE,PropStg)); OleCheck(PropStg.Enum(PropEnum)); I:=0; hr:=PropEnum.Next(1,PropStat,nil); 而hr=S_OK do 开始 公司(一); 设置长度(PropSpec,I); PropSpec[i-1]。ulKind:=PRSPEC_-PROPID; PropSpec[i-1].propid:=PropStat.propid; hr:=PropEnum.Next(1,PropStat,nil); 结束; 设定长度(PropVariant,i); Rslt:=PropStg.ReadMultiple(i,@PropSpec[0],@PropVariant[0]); 如果Rslt=S_FALSE,则退出; 对于k:=0到i-1 do 开始 S:=''; 如果PropVariant[k].vt=vt_LPSTR,则 如果已分配(PropVariant[k].pszVal),则 S:=PropVariant[k].pszVal; S:=格式(propertypidtocation(PropSpec[k].Propid)+'%S',[S]); 如果是S“”,则结果:=结果+S+#13; 结束; 最后 结束;

更多。另外:演示如何操作部分答案:可以找到设置属性的Delphi代码。 或者,如果您有最新的JCL库,请使用 警告:请注意,此代码适用于xls文件,但似乎不适用于Windows 7 Pro/Enterprise或2008(64位)中的txt/cvs和jpg文件。
M$似乎改变了这些操作系统中属性的工作方式:。遗憾的是,返回XP模式不是一个选项。

您是否尝试过Unicode Delphi中的一些示例?我曾在Delphi 2009中尝试过同样的代码,在
propsettg.Open
like上获得
%1 not found
错误。@TLama-我不久前仅在Delphi 7中,在Windows XP上尝试过,效果良好。这就是发布此问题代码的原因。我不知道这个问题。如果不起作用,OP可以发表评论,我将删除answer@RBA事实是