关于TSearchRec的使用

今天有人在QQ里问

var
  SearchRec: TSearchRec;
begin
  ...
  if(SearchRec.Attr = faAnyFile) then ...
end;

这段代码为什么不能正确运行。
我看了一下TSearchRec的帮助,里面有两段话很重要:

Note:

The faReadOnly constant has the same name as the enumerated value that is defined by the

TFieldAttribute type. If both the SysUtils and the Db units are used in your source

files, you must disambiguate by specifying the unit to qualify the use of faReadOnly.

That is, write SysUtils.faReadOnly (Delphi) or SysUtils::faReadOnly (C++).

To test for an attribute, combine the value of the Attr field with the attribute constant

with the and operator. If the file has that attribute, the result will be greater than 0.

For example, if the found file is a hidden file, the following expression will evaluate

to true:

 (SearchRec.Attr and faHidden) <> 0
 (SearchRec.Attr & faHidden) != 0

第一段是说在SysUtils中声明faReadOnly常量和在Db文件中声明的枚举值重名了。所以假如你的源代

码中同时使用了这两个单元文件,请在faReadOnly前加上限定符。在Delphi中的方法是

SysUtils.faReadOnly,在C++中的方法是SysUtils::faReadOnly。

第二段的大意是说如果需要测试Attr域中保存的文件属性时,请使用“与”操作,如果文件的属性符

合你的需要,那么返回值会大于0。下面给出了Delphi和C++的例子。
 (SearchRec.Attr and faHidden) <> 0
 (SearchRec.Attr & faHidden) != 0

这就是那位网友的代码失败的原因。

转载于:https://www.cnblogs.com/monkeyking/archive/2005/05/19/159115.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值