Function Copy_Dir(SourceDir,DestDir:String;nLx:Integer):Boolean; Var Opstruc: TshFileOpStruct; frombuf,tobuf: Array[0..128] of Char; begin FillChar(frombuf,Sizeof(frombuf),0); FillChar(tobuf,Sizeof(tobuf),0); StrPcopy(frombuf,SourceDir); Case nLx of 1: StrPcopy(tobuf,DestDir); end; With Opstruc Do Begin Wnd:=0; Case nLx of 1: wFunc:=FO_COPY; 2: wFunc:=FO_DELETE; Else wFunc:=FO_COPY; end; pFrom:=@frombuf; pTo:=@tobuf; fFlags:=FOF_NOCONFIRMATION; fAnyOperationsAborted:=False; hNameMappings:=Nil; lpszProgressTitle:=Nil; end; try ShFileOperation(OpStruc); Result:=True; except Result:=False; end; end;
我昨天在网上看到了一段代码,但是我不知道怎么设置相对路径?怎么能把StrPCopy(FromDir,'C:\Documents and Settings\Administrator\桌面\ssss\db1.mdb');里的路径换成相对路径。高手指点?实在是着急啊 ------------------------------------------------------------------------------- procedure TForm1.Button1Click(Sender: TObject); var OpStru:TSHFileOpStruct; FromDir,ToDir:array[0..128]of char; begin FillChar(FromDir,Sizeof(FromDir),0); StrPCopy(FromDir,'C:\Documents and Settings\Administrator\桌面\ssss\db1.mdb'); FillChar(ToDir,Sizeof(ToDir),0); SaveDialog1.FileName:='目的数据库路径及数据库名'; //可以改名,需saveDialog控件 SaveDialog1.Filter:='Access(*.mdb)|*.mdb'; if SaveDialog1.Execute then begin SaveDialog1.FileName:=SaveDialog1.FileName+'.mdb'; strPCopy(ToDir,SaveDialog1.FileName); With OpStru do begin Wnd:=Handle; wFunc:=FO_COPY; pFrom:=@FromDir; pTo:=@ToDir; fFlags:=FOF_FilesOnly; //FOF_NOCONFIRMATION or FOF_RENAMEONCOLLISION; fAnyOperationsAborted:=False; end; try ShFileOperation(OpStru); Showmessage('数据备份成功!'); except Showmessage('数据备份失败!'); end; end; end;
问题解决,更改后的代码是这样的。 ---------------------------------------------------------------------- var Form1: TForm1;implementation uses ShellAPI;{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject); var OpStru:TSHFileOpStruct; FromDir,ToDir:array[0..128]of char; begin FillChar(FromDir,Sizeof(FromDir),0); StrPCopy(FromDir,ExtractFilePath(Application.ExeName)+'db1.mdb'); FillChar(ToDir,Sizeof(ToDir),0); SaveDialog1.FileName:='目的数据库路径及数据库名'; //可以改名,需saveDialog控件 SaveDialog1.Filter:='Access(*.mdb)|*.mdb'; if SaveDialog1.Execute then begin SaveDialog1.FileName:=SaveDialog1.FileName+'.mdb'; strPCopy(ToDir,SaveDialog1.FileName); With OpStru do begin Wnd:=Handle; wFunc:=FO_COPY; pFrom:=@FromDir; pTo:=@ToDir; fFlags:=FOF_FilesOnly; //FOF_NOCONFIRMATION or FOF_RENAMEONCOLLISION; fAnyOperationsAborted:=False; end; try ShFileOperation(OpStru); Showmessage('数据备份成功!'); except Showmessage('数据备份失败!'); end; end; end;
Var
Opstruc: TshFileOpStruct;
frombuf,tobuf: Array[0..128] of Char;
begin
FillChar(frombuf,Sizeof(frombuf),0);
FillChar(tobuf,Sizeof(tobuf),0);
StrPcopy(frombuf,SourceDir);
Case nLx of
1:
StrPcopy(tobuf,DestDir);
end;
With Opstruc Do
Begin
Wnd:=0;
Case nLx of
1: wFunc:=FO_COPY;
2: wFunc:=FO_DELETE;
Else wFunc:=FO_COPY;
end;
pFrom:=@frombuf;
pTo:=@tobuf;
fFlags:=FOF_NOCONFIRMATION;
fAnyOperationsAborted:=False;
hNameMappings:=Nil;
lpszProgressTitle:=Nil;
end;
try
ShFileOperation(OpStruc);
Result:=True;
except
Result:=False;
end;
end;
Copy_Dir('源文件夹路经','目标文件夹路经',1)
其中原文件的路径怎么获得?我的数据库文件和执行文件放在一起,能保证我的这个文件夹放在哪里执行都可以得到原文件路径..
ExtractFilePath(Application.ExeName)
得到
-------------------------------------------------------------------------------
procedure TForm1.Button1Click(Sender: TObject);
var OpStru:TSHFileOpStruct;
FromDir,ToDir:array[0..128]of char;
begin
FillChar(FromDir,Sizeof(FromDir),0);
StrPCopy(FromDir,'C:\Documents and Settings\Administrator\桌面\ssss\db1.mdb');
FillChar(ToDir,Sizeof(ToDir),0);
SaveDialog1.FileName:='目的数据库路径及数据库名'; //可以改名,需saveDialog控件
SaveDialog1.Filter:='Access(*.mdb)|*.mdb';
if SaveDialog1.Execute then begin
SaveDialog1.FileName:=SaveDialog1.FileName+'.mdb';
strPCopy(ToDir,SaveDialog1.FileName);
With OpStru do begin
Wnd:=Handle;
wFunc:=FO_COPY;
pFrom:=@FromDir;
pTo:=@ToDir;
fFlags:=FOF_FilesOnly; //FOF_NOCONFIRMATION or FOF_RENAMEONCOLLISION;
fAnyOperationsAborted:=False;
end;
try
ShFileOperation(OpStru);
Showmessage('数据备份成功!');
except
Showmessage('数据备份失败!');
end;
end;
end;
----------------------------------------------------------------------
var
Form1: TForm1;implementation
uses ShellAPI;{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var OpStru:TSHFileOpStruct;
FromDir,ToDir:array[0..128]of char;
begin
FillChar(FromDir,Sizeof(FromDir),0);
StrPCopy(FromDir,ExtractFilePath(Application.ExeName)+'db1.mdb');
FillChar(ToDir,Sizeof(ToDir),0);
SaveDialog1.FileName:='目的数据库路径及数据库名'; //可以改名,需saveDialog控件
SaveDialog1.Filter:='Access(*.mdb)|*.mdb';
if SaveDialog1.Execute then begin
SaveDialog1.FileName:=SaveDialog1.FileName+'.mdb';
strPCopy(ToDir,SaveDialog1.FileName);
With OpStru do begin
Wnd:=Handle;
wFunc:=FO_COPY;
pFrom:=@FromDir;
pTo:=@ToDir;
fFlags:=FOF_FilesOnly; //FOF_NOCONFIRMATION or FOF_RENAMEONCOLLISION;
fAnyOperationsAborted:=False;
end;
try
ShFileOperation(OpStru);
Showmessage('数据备份成功!');
except
Showmessage('数据备份失败!');
end;
end;
end;
就可用TSHFileOpStruct和ShFileOperation了,HELP see MSDN