设置文件属性对文件的现有内容没有影响。BOM 消失的唯一方法是将文件的内容复制到省略 BOM 的新文件中。设置属性不会这样做。
请记住,您正在使用相对文件路径,因此您的机器上可能有多个文件副本并且正在查看错误的文件。始终使用完整路径。
将 BOM 和文本写入文件的更简单方法TEncoding
是使用TStreamWriter
类。
您应该FileSetAttr()
在关闭文件后调用以确保它真正生效,并且您需要在调用FileGetAttr()
之前调用FileSetAttr()
以确保正确保留现有属性。
试试这个:
procedure TForm1.Button1Click(Sender: TObject);
const
cFilename = 'c:\path to\myfile.txt';
var
sw : TStreamWriter;
Attrs: Integer;
begin
sw := TStreamWriter.Create(cFilename, False, TEncoding.UTF8);
try
sw.WriteLine('Dummy string');
finally
sw.Free;
end;
Attrs := FileGetAttr(cFilename);
if Attrs <> -1 then
FileSetAttr(cFilename, Attrs or faReadOnly);
end;
或者:
// GetFileInformationByHandle() is declared in Windows.pas, but SetFileInformationByHandle() is not!
type
_FILE_INFO_BY_HANDLE_CLASS = (
FileBasicInfo,
FileStandardInfo,
FileNameInfo,
FileRenameInfo,
FileDispositionInfo,
FileAllocationInfo,
FileEndOfFileInfo,
FileStreamInfo,
FileCompressionInfo,
FileAttributeTagInfo,
FileIdBothDirectoryInfo
);
FILE_INFO_BY_HANDLE_CLASS = _FILE_INFO_BY_HANDLE_CLASS;
_FILE_BASIC_INFO = record
CreationTime: LARGE_INTEGER;
LastAccessTime: LARGE_INTEGER;
LastWriteTime: LARGE_INTEGER;
ChangeTime: LARGE_INTEGER;
FileAttributes: DWORD;
end;
FILE_BASIC_INFO = _FILE_BASIC_INFO;
function SetFileInformationByHandle(hFile: THandle; FileInformationClass: FILE_INFO_BY_HANDLE_CLASS; lpFileInformation: Pointer; dwBufferSize: DWORD): BOOL; stdcall; external 'kernel32' delayed;
procedure TForm1.Button1Click(Sender: TObject);
const
cFilename = 'c:\path to\myfile.txt';
var
sw : TStreamWriter;
fi: TByHandleFileInformation;
bi: FILE_BASIC_INFO;
Attrs: Integer;
AttrsSet: Boolean;
begin
AttrsSet := False;
sw := TStreamWriter.Create(cFilename, False, TEncoding.UTF8);
try
sw.WriteLine('Dummy string');
if CheckWin32Version(6, 0) then
begin
if GetFileInformationByHandle(TFileStream(sw.BaseStream).Handle, fi) then
begin
bi.CreationTime.LowPart := fi.ftCreationTime.dwLowDateTime;
bi.CreationTime.HighPart := fi.ftCreationTime.dwHighDateTime;
bi.LastAccessTime.LowPart := fi.ftLastAccessTime.dwLowDateTime;
bi.LastAccessTime.HighPart := fi.ftLastAccessTime.dwHighDateTime;
bi.LastWriteTime.LowPart := fi.ftLastWriteTime.dwLowDateTime;
bi.LastWriteTime.HighPart := fi.ftLastWriteTime.dwHighDateTime;
bi.ChangeTime := bi.LastWriteTime;
bi.FileAttributes := fi.dwFileAttributes or FILE_ATTRIBUTE_READONLY;
AttrsSet := SetFileInformationByHandle(TFileStream(sw.BaseStream).Handle, FileBasicInfo, @bi, SizeOf(bi));
end;
finally
sw.Free;
end;
if not AttrsSet then
begin
Attrs := FileGetAttr(cFilename);
if Attrs <> -1 then
FileSetAttr(cFilename, Attrs or faReadOnly);
end;
end;