Delphi 文件的操作:重命名、复制、移动、删除
第一种方法:
RenameFile('Oldname', 'Newname'); CopyFile(PChar('Oldname'), PChar('Newname'), False); MoveFile(PChar('Oldname'), PChar('Newname')); DeleteFile(文件名);
第二种方法:
在Delphi可以使用ShellApi单元中的函数SHFileOperation来实现消息上传控件的制作,
SHFileOperation函数可以实现各种文件操作,只需将文件操作命令(拷贝、剪切、删除、重命名)发送给
它,它就会实现Windows资源管理器那样的文件操作功能。该函数的声明如下:
function SHFileOperation(constract lpFileOp : LPSHFILEOPSTRUCT): Integer;stdcall; LPSHFILEOPSTRUCT的结构类型: typedef struct _SHFILEOPSTRUCT{ HWND hwnd; // 显示对话框的句柄 UINT wFunc; // 指明操作类型,支持4种操作:FO_COPY拷贝、FO_MOVE剪切、 FO_DELETE删除、FO_RENAME重命名。 LPCSTR pFrom; // 源文件路径,可以是多个文件 LPCSTR pTo; // 目标路径,可以是路径或文件名,FO_DELETE时,该参数不起作用 FILEOP_FLAGS fFlags; // 标志,附加的风格选项 BOOL fAnyOperationsAborted; // 是否可被中断 LPVOID hNameMappings; // 文件映射名字,可在其它 Shell 函数中使用 LPCSTR lpszProgressTitle; // 只在 FOF_SIMPLEPROGRESS 时,指定对话框的标题。 }SHFILEOPSTRUCT;
例如:
uses ShellAPI; type TFileCommand=(fcCopy,fcMove,fcDelete,fcRename); procedure TForm1.FileOperation(aCommand: FileCommand; var aFromFile, aToFile: String); var FileOp: TSHFileOPStruct; begin ZeroMemory(@FileOp, sizeof(FileOp)); FileOp.Wnd := Form1.Handle; //显示一个进度对话框,但不显示文件名。 FileOp.fFlags := FOF_SimpleProgress; //String类型转换到PAnsiChar类型,需要经过AnsiString类型 FileOp.pFrom := PAnsiChar( AnsiString(aFromFile)); FileOp.pTo := PAnsiChar( AnsiString(aToFile)); case aCommand of fcCopy: FileOp.wFunc := FO_COPY; // 复制文件 fcMove: FileOp.wFunc := FO_MOVE; // 移动文件 fcDelete: FileOp.wFunc := FO_DELETE; // 删除文件 fcRename: FileOp.wFunc := FO_RENAME; // 重命名文件 end; SHFileOperation(FileOp); end;
Delphi 判断文件是否存在,是否正在使用
function IsFileInUse(fName: string): boolean; var HFileRes: HFILE; begin Result := false; if not FileExists(fName) then //如果文件不存在 exit; HFileRes := CreateFile(pchar(fName), GENERIC_READ or GENERIC_WRITE, 0 { this is the trick!}, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); Result := (HFileRes = INVALID_HANDLE_VALUE); if not Result then CloseHandle(HFileRes); end; 调用 procedure TForm1.Button1Click(Sender: TObject); begin if OpenDialog1.Execute then begin if IsFileInUse(OpenDialog1.FileName) = true then showmessage('文件正在使用') else showmessage('文件没有使用'); end; end;
Delphi删除或移动正在使用的文件
Delphi删除文件容易,但删除正在使用的文件,那就需要手段了,因为正在使用的文件是不允许被删除的,看代码:
unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls; const FILE_DELETE=1; FILE_RENAME=2; type TForm1 = class(TForm) Button1: TButton; Label1: TLabel; Label2: TLabel; RadioGroup1: TRadioGroup; Edit1: TEdit; Edit2: TEdit; Button2: TButton; Button3: TButton; OpenDialog1: TOpenDialog; procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); procedure Button1Click(Sender: TObject); procedure Edit2Change(Sender: TObject); procedure RadioGroup1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation { $R *.DFM} function DeleteRenameFileAfterBoot(lpFileNameToSrc,lpFileNameToDes: PChar;flag:Uint): Boolean; var WindowsDirs: array [0..MAX_PATH + 1] of Char; lpDirSrc,lpDirDes: array [0..MAX_PATH + 1] of Char; VerPlatForm: TOSVersionInfoA; StrLstDelte: TStrings; filename,s :String; i:integer; begin Result := FALSE; ZeroMemory(@VerPlatForm, SizeOf(VerPlatForm)); VerPlatForm.dwOSVersionInfoSize := SizeOf(VerPlatForm); GetVersionEx(VerPlatForm); if VerPlatForm.dwPlatformId = VER_PLATFORM_WIN32s then begin SetLastError(ERROR_NOT_SUPPORTED); Exit; end else if VerPlatForm.dwPlatformId = VER_PLATFORM_WIN32_NT then begin if flag=FILE_DELETE then Result := MoveFileEx(PChar(lpFileNameToSrc), nil, MOVEFILE_REPLACE_EXISTING + MOVEFILE_DELAY_UNTIL_REBOOT) else if (flag=FILE_RENAME) then Result := MoveFileEx(lpFileNameToSrc, lpFileNameToDes, MOVEFILE_REPLACE_EXISTING + MOVEFILE_DELAY_UNTIL_REBOOT); end else begin StrLstDelte := TStringList.Create; GetWindowsDirectory(WindowsDirs, MAX_PATH + 1); filename:=WindowsDirs; if filename[length(filename)]<>'\' then filename:=filename+'\'; filename:=filename+'wininit.ini'; if FileExists(filename) then StrLstDelte.LoadFromFile(filename); if StrLstDelte.IndexOf('[rename]') &