我以前写的一个小程序的一部分
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DB, ADODB, ComCtrls,shellapi, ExtCtrls, ImgList;
type
TForm1 = class(TForm)
ADOConnection1: TADOConnection;
ADOQuery1: TADOQuery;
OpenDialog1: TOpenDialog;
Button1: TButton;
Button2: TButton;
Button3: TButton;
SaveDialog1: TSaveDialog;
TreeView1: TTreeView;
ImageList1: TImageList;
Label1: TLabel;
Timer1: TTimer;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure TreeView1DblClick(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure FormClose(Sender: TObject
var Action: TCloseAction);
private
procedure relist();
function ExecuteFile(AppHandle:integer
const FileName,
Params, DefaultDir: string
ShowCmd: Integer;
var ProcessHandle: Longint): THandle;
function GetFileLastAccessTime(sFileName:string;uFlag:integer):TDateTime;
function IsFileInUse(fName : string) : boolean;
public
{ Public declarations }
end;
var
Form1: TForm1;
filenameex:string;
implementation
{$R *.dfm}
function tform1.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;
function Tform1.GetFileLastAccessTime(sFileName:string;uFlag:integer):TDateTime
//获得文件的创建、修改及最后访问时间
var
ffd:TWin32FindData;
dft
Word;
lft:TFileTime;
h:THandle;
begin
h:=FindFirstFile(PChar(sFileName),ffd);
if h<>INVALID_HANDLE_value then
begin
case uFlag of
1:FileTimeToLocalFileTime(ffd.ftCreationTime,lft);
2:FileTimeToLocalFileTime(ffd.ftLastWriteTime,lft);
3:FileTimeToLocalFileTime(ffd.ftLastAccessTime,lft);
else
FileTimeToLocalFileTime(ffd.ftLastAccessTime,lft);
end;
FileTimeToDosDateTime(lft,LongRec(dft).Hi,LongRec(dft).Lo);
Result:=FileDateToDateTime(dft);
windows.FindClose(h);
end
else
result:=0;
end;
function TForm1.ExecuteFile(AppHandle:integer
const FileName, //执行外部文件
Params, DefaultDir: string
ShowCmd: Integer;
var ProcessHandle: Longint): THandle;
var
info: TShellExecuteInfoA;
begin
info.cbSize := sizeof(info);
info.Wnd := AppHandle;
info.lpFile := PChar(FileName);
info.lpVerb := PChar('open');
info.lpParameters := PChar(params);
info.lpDirectory := PChar(DefaultDir);
info.nShow := ShowCmd;
info.fMask := SEE_MASK_NOCLOSEPROCESS or SEE_MASK_FLAG_NO_UI;
ShellExecuteEx(@info);
ProcessHandle := info.hProcess;
result := info.hInstApp;
end;
procedure tform1.relist()
//刷新treeview
var
i:integer;
TreeNodes:TTreeNodes;
begin
treeview1.Items.Clear;
TreeNodes:=TreeView1.Items;
adoquery1.Close;
adoquery1.sql.text:='select * from myfile where fileowner=''lijp'' ';
adoquery1.open;
for i:=0 to adoquery1.RecordCount-1 do
begin
TreeNodes.Add(Nil,adoquery1.fieldbyname('filename').AsString);
adoquery1.Next;
end;
end;
procedure TForm1.Button1Click(Sender: TObject)
//上传文件
var FileName :String;
begin
if opendialog1.Execute then
if opendialog1.FileName<>'' then
begin
FileName := ExtractFileName(OpenDialog1.FileName);
with adoquery1 do
begin
Close;
Sql.text:='INSERT INTO myfile (filename,savefile,fileowner) VALUES
FileName,:FileSource,:fileowner)';
Parameters.ParamByName('FileName').Value:=filename;// .AsString:= FileName;
Parameters.ParamByName('FileSource').LoadFromFile(OpenDialog1.FileName,ftBlob);
parameters.parambyname('fileowner').value:='lijp';
Prepared;
ExecSQL;
application.ProcessMessages;
messagebox(0,'文档上传完毕!','提示',0);
self.relist;
end;
end;
end;
procedure TForm1.Button2Click(Sender: TObject)
//update文件
var
FileName,filedir :String;
begin
if savedialog1.Execute then
if (savedialog1.FileName<>'')and (treeview1.Selected<>nil) then
begin
filedir:= ExtractFileName(savedialog1.FileName);
with adoquery1 do
begin
Close;
Sql.text:=format('SELECT * FROM myfile WHERE FileName = ''%s''',[treeview1.selected.text]);;
Open;
FileName :=FieldByName('FileName').AsString;
(FieldByName('savefile') AS TBlobField).SaveToFile(FileName);
end;
end;
end;
procedure TForm1.Button3Click(Sender: TObject)
//下载文件
var FileName :String;
begin
if opendialog1.Execute then
if opendialog1.FileName<>'' then
begin
FileName := ExtractFileName(OpenDialog1.FileName);
with adoquery1 do
begin
close;
sql.text:='select * from myfile';
open;
Edit;
fieldbyname('filename').Value:=filename;
(FieldByName('savefile') AS TBlobField).LoadFromFile(OpenDialog1.FileName);
post;
messagebox(0,'文档更新完毕!','提示',0);
self.relist;
end;
end;
end;
procedure TForm1.FormCreate(Sender: TObject)
//系统初始化时建一个为‘c:/mypdm'的放文件的临时目录
begin
{$I-}
mkdir('c:/mypdm');
setCurrentDir('c:/mypdm');
{$I+}
self.relist;
end;
procedure TForm1.TreeView1DblClick(Sender: TObject)
//双击treeeview时下载文件,并使其可以 编辑,发生修改后提示,是否保存.
var
FileName,filedir:String;
iProcessHandle:integer;
i:integer;
begin
// filedir:='c:/mypdm/';
with adoquery1 do
begin
Close;
Sql.text:=format('SELECT * FROM myfile WHERE FileName = ''%s''',[treeview1.selected.text]);;
Open;
FileName :=FieldByName('FileName').AsString;
(FieldByName('savefile') AS TBlobField).SaveToFile(filedir+FileName);
end;
ExecuteFile(self.Handle,filename,'','',SW_SHOW,iProcessHandle);
for i:=0 to 100 do
begin
end;
adoquery1.Close;
adoquery1.SQL.Text:=format('update myfile set updatetime=''%s'' where filename=''%s''',[datetimetostr(getfilelastaccesstime(filename,2)),treeview1.selected.text]);
adoquery1.execsql;
memo1.Lines.Add(adoquery1.SQL.Text);
filenameex:=filename;
self.Timer1.Enabled:=true;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
label1.Caption:=datetimetostr(getfilelastaccesstime(filenameex,2));
// if not IsFileInUse(filenameex) then
adoquery1.Close;
adoquery1.SQL.Text:=format('select count(*) as count from myfile where filename=''%s'' and updatetime<''%s''',[filenameex,datetimetostr(getfilelastaccesstime(filenameex,2))]);
adoquery1.Open;
memo1.Lines.Add(adoquery1.SQL.Text);
if (adoquery1.FieldByName('count').AsInteger=1) and not(IsFileInUse(filenameex)) then
// if FileExists(filenameex) then
begin
self.Timer1.Enabled:=false;
if messagedlg('是否保存?',mtconfirmation,[mbyes,mbno],0)=mryes then
with adoquery1 do
begin
close;
sql.text:=format('select * from myfile where filename=''%s''',[filenameex]);
open;
Edit;
(FieldByName('savefile') AS TBlobField).LoadFromFile(filenameex);
fieldbyname('updatetime').Value:=datetimetostr(now);
post;
end;
end;
// end;
end;
procedure TForm1.FormClose(Sender: TObject
var Action: TCloseAction)
//关闭系统时清空'c:/mypdm'文件夹上的所有检出的文件
var
sr: TSearchRec;
FileAttrs: Integer;
begin
fileattrs:=faAnyFile;
if FindFirst('c:/mypdm/*.*', FileAttrs, sr) = 0 then
begin
repeat
if (sr.Attr and FileAttrs) = sr.Attr then
begin
DeleteFile(sr.Name);
end;
until FindNext(sr) <> 0;
FindClose(sr);
end
end;
end.