打印本文 打印本文 关闭窗口 关闭窗口
Delphi例程-文件管理例程(1~15)
作者:武汉SEO闵涛  文章来源:敏韬网  点击数1786  更新时间:2009/4/23 18:35:21  文章录入:mintao  责任编辑:mintao
;     else
        NewAttributes := NewAttributes and not SysUtils.faReadOnly;
      if Archive.Checked then
        NewAttributes := NewAttributes or faArchive
      else
        NewAttributes := NewAttributes and not faArchive;
      if System.Checked then
        NewAttributes := NewAttributes or faSysFile
      else
        NewAttributes := NewAttributes and not faSysFile;
      if Hidden.Checked then
        NewAttributes := NewAttributes or faHidden
      else
        NewAttributes := NewAttributes and not faHidden;
      if NewAttributes <> Attributes then { if anything changed... }
        FileSetAttr(FileDirName.Caption, NewAttributes);
         { ...write the new values }
    end;
  end;
end;
 
14.FileClose 过程 
 关闭指定的文件
单元
 SysUtils
语法
 procedure FileClose(Handle: Integer);
描述
 FileClose 关闭给定文件句柄的文件。句柄在文件使用FileOpen或FileCreate打开时获得。
 当和Delphi语言的文件变量一起使用时,应使用 CloseFile 过程代替。
示例
 下面的例子使用了一个按钮,一个字符串栅格和一个保存对话框在窗体上。单击按钮后,用户将被提示输入文件名。当用户单OK后,字符串栅格的内容将被写到指定的文件中。附加的信息同样被写到文件中,以便文件能够使用FileRead函数容易地读取。
procedure TForm1.Button1Click(Sender: TObject);
var
  BackupName: string;
  FileHandle: Integer;
  StringLen: Integer;
  X: Integer;
  Y: Integer;
begin
  if SaveDialog1.Execute then
  begin
    if FileExists(SaveDialog1.FileName) then
    begin
      BackupName := ExtractFileName(SaveDialog1.FileName);
      BackupName := ChangeFileExt(BackupName, ''''.BAK'''');
      if not RenameFile(SaveDialog1.FileName, BackupName) then
        raise Exception.Create(''''Unable to create backup file.'''');
    end;
    FileHandle := FileCreate(SaveDialog1.FileName);
    { Write out the number of rows and columns in the grid. }
    FileWrite(FileHandle,
      StringGrid1.ColCount, SizeOf(StringGrid1.ColCount));
    FileWrite(FileHandle,
      StringGrid1.RowCount, SizeOf(StringGrid1.RowCount));
    for X := 0 to StringGrid1.ColCount - 1 do
    begin
      for Y := 0 to StringGrid1.RowCount - 1 do
      begin
        { Write out the length of each string, followed by the string itself. }
        StringLen := Length(StringGrid1.Cells[X,Y]);
        FileWrite(FileHandle, StringLen, SizeOf(StringLen));
        FileWrite(FileHandle,
          StringGrid1.Cells[X,Y], Length(StringGrid1.Cells[X,Y]);
      end;
    end;
    FileClose(FileHandle);
  end;
end;

15.FileCreate 函数 
 创建一个新文件
单元
 SysUtils
语法
 function FileCreate(const FileName: string): Integer; overload;
 function FileCreate(const FileName: string; Rights: Integer): Integer; overload;
描述
 FileCreate 用指定的名称创建新文件。如果返回值是正数,说明函数成功而且值是新文件的句柄。返回值是-1说明有错误发生。
 在 Windows中,FileAccessRights 变量和 Rights 参数被忽略。

上一页  [1] [2] 

打印本文 打印本文 关闭窗口 关闭窗口