11. juni 2001 - 14:06Der er
7 kommentarer og 2 løsninger
Sæt filer til ReadOnly i Windows (HowTo).
Hej.
Jeg har nogle filer, som jeg gerne vil sætte til readOnly ude i Windows fra Delphi altså. Altså få Delphi til at gå ind i Properties på den enkelte fil og sætte den til ReadOnly.
Direkte fra hjælpen: <SNIP> The SetFileAttributes function sets a file\'s attributes.
BOOL SetFileAttributes(
LPCTSTR lpFileName, // address of filename DWORD dwFileAttributes // address of attributes to set );
Parameters
lpFileName
Points to a string that specifies the name of the file whose attributes are to be set. Windows 95: This string must not exceed MAX_PATH characters. Windows NT: There is a default string size limit for paths of MAX_PATH characters. This limit is related to how the SetFileAttributes function parses paths. An application can transcend this limit and send in paths longer than MAX_PATH characters by calling the wide (W) version of SetFileAttributes and prepending \"\\\\?\\\" to the path. The \"\\\\?\\\" tells the function to turn off path parsing; it lets paths longer than MAX_PATH be used with SetFileAttributesW. This also works with UNC names. The \"\\\\?\\\" is ignored as part of the path. For example, \"\\\\?\\C:\\myworld\\private\" is seen as \"C:\\myworld\\private\", and \"\\\\?\\UNC\\bill_g_1\\hotstuff\\coolapps\" is seen as \"\\\\bill_g_1\\hotstuff\\coolapps\".
dwFileAttributes
Specifies the file attributes to set for the file. This parameter can be a combination of the following values. However, all other values override FILE_ATTRIBUTE_NORMAL.
Value Meaning FILE_ATTRIBUTE_ARCHIVE The file is an archive file. Applications use this value to mark files for backup or removal. FILE_ATTRIBUTE_HIDDEN The file is hidden. It is not included in an ordinary directory listing. FILE_ATTRIBUTE_NORMAL The file has no other attributes set. This value is valid only if used alone. FILE_ATTRIBUTE_OFFLINE The data of the file is not immediately available. Indicates that the file data has been physically moved to offline storage. FILE_ATTRIBUTE_READONLY The file is read-only. Applications can read the file but cannot write to it or delete it. FILE_ATTRIBUTE_SYSTEM The file is part of the operating system or is used exclusively by it. FILE_ATTRIBUTE_TEMPORARY The file is being used for temporary storage. File systems attempt to keep all of the data in memory for quicker access rather than flushing the data back to mass storage. A temporary file should be deleted by the application as soon as it is no longer needed.
Return Values
If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error information, call GetLastError.
Remarks
You cannot use the SetFileAttribute function to set a file\'s compression state. Setting FILE_ATTRIBUTE_COMPRESSED in the dwFileAttributes parameter does nothing. Use the DeviceIoControl function and the FSCTL_SET_COMPRESSION operation to set a file\'s compression state. </SNIP>
The following code reads a file\'s attributes into a set variable, sets the check boxes in a file-attribute dialog box to represent the current attributes, then executes the dialog box. If the user changes and accepts any dialog box settings, the code sets the file attributes to match the changed settings:
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 and not 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;
Ja. Jeg kan nu ikke finde SetFileAttributes. Jeg ved det lydder underligt ! Jeg kan til gengæld finde en der hedder \'FileSetAttr\'. Den lydder som om den skulle kunne bruges, men kan nogle forklare mig den nedenstående kode? (eksempel).
jeg er med på at den pågældende files attributes læses over i var \'Attrs. Men hvad sker er så? Som sagt før vil jeg gerne sætte ReadOnly til True. ------------------------------------------------- Attrs := FileGetAttr(\'MyFile.sys\'); if Attrs and faHidden <> 0 then FileSetAttr(\'MyFile.sys\', Attrs – faHidden); -------------------------------------------------
Brug Delphi\'s egen! Det ER taget fra Delphi\'s egen hjælp, det jeg har fundet. Det er også meget godt at i klipper ud derfra, men ved i reelt hvordan man gør? Hvis jeg må være så fræk. Jeg ved ikke hvor meget i får ud af 1 A4 side. Jeg skal bare have sat ReadOnly = True for en file. Det kan kun fylde et par linier.
Ja du må gerne være så fræk. Delphi\'s hjælp er \"tvedelt\". Foruden hjælpen til Borland/Delphi, finder du også hjælp til Windows API (Help->Windows SDK). Det er ikke så svært <G> her har du et DELPHI EKSEMPEL på hvordan den kan bruges:
Ja i må altså undskylde, hvis i blev lidt fornærmede, over min måde at sige det på. *S* Men jeg vil sige tunsinde tak for hjælpen. Mon ikke jeg kan gøre det nu. Men nogle gange kan det være svært at se skoven for bare træer, mht til A4 side med kode.
function TFile.GetFileAttributes: LongInt; var Win32FindData : TWin32FindData; Handle : THandle; begin Result:=-1; // Returns -1 if the functions fails Handle := Windows.FindFirstFile(PChar(FFilename), Win32FindData); if INVALID_HANDLE_VALUE <> Handle then begin Result :=0; Windows.FindClose( Handle ); if (Win32FindData.dwFileAttributes and faReadOnly) = faReadOnly then Result := Result or faReadOnly;
if (Win32FindData.dwFileAttributes and faHidden) = faHidden then Result := Result or faHidden;
if (Win32FindData.dwFileAttributes and faSysFile) = faSysFile then Result := Result or faSysFile;
if (Win32FindData.dwFileAttributes and faVolumeID) = faVolumeID then Result := Result or faVolumeID;
if (Win32FindData.dwFileAttributes and faDirectory) = faDirectory then Result := Result or faDirectory;
if (Win32FindData.dwFileAttributes and faArchive) = faArchive then Result := Result or faArchive; end; end;
function TFile.GetFileCreationTimeTime: TDateTime; var DosFileTime : DWORD; LocalFileTime : TFileTime; Handle : THandle; begin Result:=-1; // Returns -1 if the functions fails Handle:=FileOpen(FFileName,fmOpenReadWrite); if INVALID_HANDLE_VALUE <> Handle then begin GetFileTime(Handle, @LocalFileTime,nil,nil); FileTimeToLocalFileTime( LocalFileTime, LocalFileTime ); FileTimeToDosDateTime(LocalFileTime ,LongRec(DosFileTime).Hi, LongRec(DosFileTime).Lo); Result := FileDateToDateTime(DosFileTime); FileClose( Handle ); end; end;
function TFile.GetFileLastAccessTime : TDateTime; var DosFileTime : DWORD; LocalFileTime : TFileTime; Handle : THandle; begin Result:=-1; // Returns -1 if the functions fails Handle:=FileOpen(FFileName,fmOpenReadWrite); if INVALID_HANDLE_VALUE <> Handle then begin GetFileTime(Handle, nil, @LocalFileTime,nil); FileTimeToLocalFileTime( LocalFileTime, LocalFileTime ); FileTimeToDosDateTime(LocalFileTime ,LongRec(DosFileTime).Hi, LongRec(DosFileTime).Lo); Result := FileDateToDateTime(DosFileTime); FileClose( Handle ); end; end;
function TFile.GetFileLastWriteTime: TDateTime; var DosFileTime : DWORD; LocalFileTime : TFileTime; Handle : THandle; begin Result:=-1; // Returns -1 if the functions fails Handle:=FileOpen(FFileName,fmOpenReadWrite); if INVALID_HANDLE_VALUE <> Handle then begin GetFileTime(Handle, @LocalFileTime,nil,nil); FileTimeToLocalFileTime( LocalFileTime, LocalFileTime ); FileTimeToDosDateTime(LocalFileTime ,LongRec(DosFileTime).Hi, LongRec(DosFileTime).Lo); Result := FileDateToDateTime(DosFileTime); FileClose( Handle ); end; end;
procedure TFile.SetFileAttributes(const Value: LongInt); begin Windows.SetFileAttributes(PChar(FFileName), Value) end;
procedure TFile.SetFileCreationTimeTime(const Value: TDateTime); var LocalFileTime, FileTime: TFileTime; Handle : THandle; Age : Longint; begin Handle:=FileOpen(FFileName,fmOpenReadWrite); Age:=DateTimeToFileDate(Value); DosDateTimeToFileTime(LongRec(Age).Hi, LongRec(Age).Lo, LocalFileTime); LocalFileTimeToFileTime(LocalFileTime, FileTime); SetFileTime(Handle,@LocalFileTime,nil, nil); FileClose(Handle); end;
procedure TFile.SetFileLastAccessTime(const Value: TDateTime); var LocalFileTime, FileTime: TFileTime; Handle : THandle; Age : Longint; begin Handle:=FileOpen(FFileName,fmOpenReadWrite); Age:=DateTimeToFileDate(Value); DosDateTimeToFileTime(LongRec(Age).Hi, LongRec(Age).Lo, LocalFileTime); LocalFileTimeToFileTime(LocalFileTime, FileTime); SetFileTime(Handle,nil, @LocalFileTime,nil); FileClose(Handle); end;
procedure TFile.SetFileLastWriteTime(const Value: TDateTime); var LocalFileTime, FileTime: TFileTime; Handle : THandle; Age : Longint; begin Handle:=FileOpen(FFileName,fmOpenReadWrite); Age:=DateTimeToFileDate(Value); DosDateTimeToFileTime(LongRec(Age).Hi, LongRec(Age).Lo, LocalFileTime); LocalFileTimeToFileTime(LocalFileTime, FileTime); SetFileTime(Handle,nil,nil,@LocalFileTime); FileClose(Handle); end;
procedure TForm1.Button1Click(Sender: TObject); begin If not OpenDialog1.Execute then Exit;
with TFile.Create(OpenDialog1.FileName) do try Label2.Caption := FileName; Edit1.Text := DateToStr(FileLastAccessTime); Edit2.Text := DateToStr(FileCreationTimeTime); Edit3.Text := DateToStr(FileLastWriteTime);
CheckBox1.Checked := Bool(GetFileAttributes and faReadOnly); CheckBox2.Checked := Bool(GetFileAttributes and faHidden); CheckBox3.Checked := Bool(GetFileAttributes and faSysFile); CheckBox4.Checked := Bool(GetFileAttributes and faVolumeID); CheckBox5.Checked := Bool(GetFileAttributes and faDirectory); CheckBox6.Checked := Bool(GetFileAttributes and faArchive); finally free; end; end;
Jens B
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.