怎么样删除只读和隐藏文件或者怎么样复制文件(100分)

  • 主题发起人 主题发起人 zcm1975117
  • 开始时间 开始时间
Z

zcm1975117

Unregistered / Unconfirmed
GUEST, unregistred user!
1。复制文件时,如果目的地已经存在,并且是只读文件,怎么覆盖此文件。
2。怎么样删除只读和隐藏文件呢?
 
1.先删除(2。),再复制
2。deleteFile(Filename)
 
或者::
API 函数 SHFileOperation 可以调用 Explorer 执行几乎所有的文件操作。

使用很简单,需要注意的是,必须 uses ShellAPI 这个单元:
WINSHELLAPI int WINAPI SHFileOperation(
LPSHFILEOPSTRUCT lpFileOp
);
关键是 LPSHFILEOPSTRUCT 结构的填写:
typedef struct _SHFILEOPSTRUCT { // shfos
HWND hwnd;
UINT wFunc;
LPCSTR pFrom;
LPCSTR pTo;
FILEOP_FLAGS fFlags;
BOOL fAnyOperationsAborted;
LPVOID hNameMappings;
LPCSTR lpszProgressTitle;
} SHFILEOPSTRUCT, FAR *LPSHFILEOPSTRUCT;

参数说明:
Members

hwnd

Handle of the dialog box to use to display information about the status of the
operation.

wFunc

Operation to perform. This member can be one of the following values:

FO_COPY Copies the files specified by pFrom to the location specified by pTo.
FO_DELETE Deletes the files specified by pFrom (pTo is ignored).
FO_MOVE Moves the files specified by pFrom to the location specified by pTo.
FO_RENAME Renames the files specified by pFrom.

pFrom
Pointer to a buffer that specifies one or more source file names. Multiple names must be null-separated. The list of names must be double null-terminated.
pTo
Pointer to a buffer that contains the name of the destination file or directory. The buffer can contain mutiple destination file names if the fFlags member specifies FOF_MULTIDESTFILES. Multiple names must be null-separated. The list of names must be double null-terminated.
fFlags
Flags that control the file operation. This member can be a combination of the following values:
FOF_ALLOWUNDO Preserves undo information, if possible.
FOF_CONFIRMMOUSE Not implemented.
FOF_FILESONLY Performs the operation only on files if a wildcard filename (*.*) is specified.
FOF_MULTIDESTFILES Indicates that the pTo member specifies multiple destination files (one for each source file) rather than one directory where all source files are to be deposited.
FOF_NOCONFIRMATION Responds with "yes to all" for any dialog box that is displayed.
FOF_NOCONFIRMMKDIR Does not confirm the creation of a new directory if the operation requires one to be created.
FOF_RENAMEONCOLLISION Gives the file being operated on a new name (such as "Copy #1 of...") in a move, copy, or rename operation if a file of the target name already exists.
FOF_SILENT Does not display a progress dialog box.
FOF_SIMPLEPROGRESS Displays a progress dialog box, but does not show the filenames.
FOF_WANTMAPPINGHANDLE Fills in the hNameMappings member. The handle must be freed by using the SHFreeNameMappings function.

fAnyOperationsAborted
Value that receives TRUE if the user aborted any file operations before they were completed or FALSE otherwise.
hNameMappings
Handle of a filename mapping object that contains an array of SHNAMEMAPPING structures. Each structure contains the old and new path names for each file that was moved, copied, or renamed. This member is used only if fFlags includes FOF_WANTMAPPINGHANDLE.
lpszProgressTitle
Pointer to a string to use as the title for a progress dialog box. This member is used only if fFlags includes FOF_SIMPLEPROGRESS.
附一段例子:
uses shellapi;
{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
Var
T:TSHFileOpStruct;
P,P1:String;
begin
P:='c:/windows/a.txt';
P1:='c:/a.txt';
With T do
Begin
Wnd:=0;
wFunc:=FO_COPY;
pFrom:=Pchar(P1);
pTo:=PChar(P);
fFlags:=FOF_NOERRORUI;//标志表明允许恢复,无须确认并不显示出错信息
hNameMappings:=nil;
lpszProgressTitle:='正在复制文件。。。';
fAnyOperationsAborted:=False;
End;
SHFileOperation(T);
end;
 
1:
procedure TForm1.Button2Click(Sender: TObject);
var


sPath:string;
fsTemp:SHFILEOPSTRUCT;
i:integer;


begin
sPath:=InputBox('文件操作','输入移动路径','c:/windows');
if sPath<>''then begin
fsTemp.Wnd := Self.Handle;
fsTemp.wFunc :=FO_MOVE;
fsTemp.fFlags :=FOF_ALLOWUNDO;
for i:=0 to ListBox1.Items.Count-1 do begin
fsTemp.pFrom := PChar(ListBox1.Items.Strings);
fsTemp.pTo := PChar(sPath);
fsTemp.lpszProgressTitle:='移动文件';
if SHFileOperation(fsTemp)<>0 then
ShowMessage('文件复制失败');
end;


end;
end;



end.



点击菜单的 Project | Build ContextMenu 项,Delphi就会建立Contextmenu.dll文件,这个就是上下文相关菜单程序了。
使用,Regsvr32.exe 注册程序,然后在Windows的Explore 中在任意的一个或者几个文件中点击鼠标右键,在上下文菜单中就会
多一个文件操作的菜单项,点击该项,在弹出窗口的列表中会列出你所选择的所有文件的文件名,你可以选择拷贝文件按钮或者
移动文件按钮执行文件操作。

2:
例子如下:// Delphi

program del;

uses ShellApi;
{ 利用ShellApi中: function SHFileOperation(const lpFileOp: TSHFileOpStruct): Integer; stdcall; }

Var T:TSHFileOpStruct;
P:String;
begin
P:='C:/Windows/System/EL_CONTROL.CPL';
With T do
Begin
Wnd:=0;
wFunc:=FO_DELETE;
pFrom:=Pchar(P);
fFlags:=FOF_ALLOWUNDO
End;
SHFileOperation(T);
End.

注意:
1. 给出文件的绝对路径名,否则可能不能恢复;
2. MS的文档说对于多个文件,每个文件名必须被#)字符分隔,而整个字符串
 
FileSetAttr(FileName,0);
Delete(FileName);
就可以把任何属性的文件删除.(包括只读,隐藏)

复制文件时,如果目的文件已经存在,将其删除,再复制.
 
取消文件的其它权限就好了
SetFileAttributes('C:/temp/test1.txt', FILE_ATTRIBUTE_NORMAL);
 
谢谢大家,其实我有一个文件在:C:/WINDOWS/SYSTEM/text.exe,它的属性为只读、
隐藏、归档、系统,我现在就想从另外一个地方复制一个文件覆盖这个文件,请问
大家怎么实现,我用了以上大家提供的方法,但是原来那个依然还是原来那文件。
 
你的“另外一个地方”指的哪里?是网络上吗?
 
应该与在那个地方没有关系吧!就是在同一台机也是如此呀!!
 
不会呀,我都试过了
uses shellapi;
{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
Var
T:TSHFileOpStruct;
P,P1:String;
begin
P:='c:/windows/a.txt';
P1:='c:/a.txt';
With T do
Begin
Wnd:=0;
wFunc:=FO_COPY;
pFrom:=Pchar(P1);
pTo:=PChar(P);
fFlags:=FOF_NOERRORUI;//标志表明允许恢复,无须确认并不显示出错信息
hNameMappings:=nil;
lpszProgressTitle:='正在复制文件。。。';
fAnyOperationsAborted:=False;
End;
SHFileOperation(T);
end;
 

Similar threads

D
回复
0
查看
787
DelphiTeacher的专栏
D
D
回复
0
查看
825
DelphiTeacher的专栏
D
D
回复
0
查看
657
DelphiTeacher的专栏
D
后退
顶部