对文件进行写操作是这样打开文件的:
stream := TFileStream.Create(filename, fmCreate);
如果是只读文件,就会报错:Cannot create file filename.
现在要判断文件是否可写,出错就报详细的错误,如:文件为只读属性
目录为只读属性
目录不存在现在的问题是,如何得到文件或者目录的属性?包括只读/共享等

解决方案 »

  1.   

    目录是否存在用DirectoryExists函数
      

  2.   

    FileSteAttr('Filename',33);设置属性
    FileGetAttr('filename');得到属性值
    procedure TFMForm.Properties1Click(Sender: TObject);
    var
      Attributes, NewAttributes: Word;
    begin
      with FileAttrForm do
      begin
        FileDirName.Caption := FileList.Items[FileList.ItemIndex];
        { set box caption }
        PathName.Caption := FileList.Directory;
        { show directory name }
        ChangeDate.Caption := 
          DateTimeToStr(FileDateToDateTime(FileAge(FileList.FileName)));
        Attributes := FileGetAttr(FileDirName.Caption);
        { read file attributes }
        ReadOnly.Checked := (Attributes and faReadOnly) = faReadOnly;
        Archive.Checked := (Attributes and faArchive) = faArchive;
        System.Checked := (Attributes and faSysFile) = faSysFile;
        Hidden.Checked := (Attributes and faHidden) = faHidden;
        if ShowModal <> id_Cancel then { execute dialog box }
        begin
          NewAttributes := Attributes;
          { start with original attributes }
          if ReadOnly.Checked then
            NewAttributes := NewAttributes or faReadOnly
          else 
            NewAttributes := NewAttributes andnot faReadOnly;
          if Archive.Checked then
            NewAttributes := NewAttributes or faArchive
          else 
            NewAttributes := NewAttributes andnot faArchive;
          if System.Checked then 
            NewAttributes := NewAttributes or faSysFile
          else 
            NewAttributes := NewAttributes andnot faSysFile;
          if Hidden.Checked then 
            NewAttributes := NewAttributes or faHidden
          else 
            NewAttributes := NewAttributes andnot faHidden;
          if NewAttributes <> Attributes then { if anything changed... }
            FileSetAttr(FileDirName.Caption, NewAttributes);
             { ...write the new values }
        end;
      end;
    end;
      

  3.   

    Returns the file attributes of FileName. UnitSysUtilsCategoryfile management routinesfunction FileGetAttr(const FileName: string): Integer;DescriptionFileGetAttr returns the attributes of the file as a string of bits. This value is the same as the Attr field of a TSearchRec record. Check for individual attributes with code such as the following:Attrs := FileGetAttr('MyFile.sys');if Attrs and faHidden <> 0 then
      FileSetAttr('MyFile.sys', Attrs ?faHidden);A return value of -1 indicates that an error occurred.Note: See TSearchRec for a description of the individual attribute constants.
      

  4.   

    FileSteAttr('Filename',33);设置属性
    FileGetAttr('filename');得到属性值
    procedure TFMForm.Properties1Click(Sender: TObject);
    var
      Attributes, NewAttributes: Word;
    begin
      with FileAttrForm do
      begin
        FileDirName.Caption := FileList.Items[FileList.ItemIndex];
        { set box caption }
        PathName.Caption := FileList.Directory;
        { show directory name }
        ChangeDate.Caption := 
          DateTimeToStr(FileDateToDateTime(FileAge(FileList.FileName)));
        Attributes := FileGetAttr(FileDirName.Caption);
        { read file attributes }
        ReadOnly.Checked := (Attributes and faReadOnly) = faReadOnly;
        Archive.Checked := (Attributes and faArchive) = faArchive;
        System.Checked := (Attributes and faSysFile) = faSysFile;
        Hidden.Checked := (Attributes and faHidden) = faHidden;
        if ShowModal <> id_Cancel then { execute dialog box }
        begin
          NewAttributes := Attributes;
          { start with original attributes }
          if ReadOnly.Checked then
            NewAttributes := NewAttributes or faReadOnly
          else 
            NewAttributes := NewAttributes andnot faReadOnly;
          if Archive.Checked then
            NewAttributes := NewAttributes or faArchive
          else 
            NewAttributes := NewAttributes andnot faArchive;
          if System.Checked then 
            NewAttributes := NewAttributes or faSysFile
          else 
            NewAttributes := NewAttributes andnot faSysFile;
          if Hidden.Checked then 
            NewAttributes := NewAttributes or faHidden
          else 
            NewAttributes := NewAttributes andnot faHidden;
          if NewAttributes <> Attributes then { if anything changed... }
            FileSetAttr(FileDirName.Caption, NewAttributes);
             { ...write the new values }
        end;
      end;
    end;
      

  5.   

    Returns the file attributes of FileName. UnitSysUtilsCategoryfile management routinesfunction FileGetAttr(const FileName: string): Integer;DescriptionFileGetAttr returns the attributes of the file as a string of bits. This value is the same as the Attr field of a TSearchRec record. Check for individual attributes with code such as the following:Attrs := FileGetAttr('MyFile.sys');if Attrs and faHidden <> 0 then
      FileSetAttr('MyFile.sys', Attrs ?faHidden);A return value of -1 indicates that an error occurred.Note: See TSearchRec for a description of the individual attribute constants.
      

  6.   

    还有:
    function FileAge(const FileName: string): Integer;
    var
      Handle: THandle;
      FindData: TWin32FindData;
      LocalFileTime: TFileTime;
    begin
      Handle := FindFirstFile(PChar(FileName), FindData);
      if Handle <> INVALID_HANDLE_VALUE then
      begin
        Windows.FindClose(Handle);
        if (FindData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) = 0 then
        begin
          FileTimeToLocalFileTime(FindData.ftLastWriteTime, LocalFileTime);
          if FileTimeToDosDateTime(LocalFileTime, LongRec(Result).Hi,
            LongRec(Result).Lo) then Exit;
        end;
      end;
      Result := -1;
    end;
      

  7.   

    procedure TForm1.Button2Click(Sender: TObject);
    begin
      if (fileGetAttr('c:\autoexec.bat') and faReadOnly)=faReadOnly then
        showMessage('文件只读!');
      if (fileGetAttr('c:\autoexec.bat') and faSysFile)=faSysFile then
        showMessage('系统只读!');
    end;其他类似。
      

  8.   

    学习,在Delphi中的联机帮助中有很多文件类的操作函数,这个我倒没注意,如果要我做我会用FindFirst找到文件后再从中TSerachRec提取文件属性的!