关于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

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

posted @ 2005-05-19 19:10  monkeyking  阅读(3139)  评论(0编辑  收藏  举报