如何判斷文字檔正在使用中 |
尚未結案
|
alice
初階會員 發表:41 回覆:49 積分:28 註冊:2002-04-30 發送簡訊給我 |
|
ralph
初階會員 發表:41 回覆:82 積分:29 註冊:2003-02-04 發送簡訊給我 |
function FileInUse(FileName: string): Boolean;
var hFileRes: HFILE;
begin
Result := False;
if not FileExists(FileName) then exit;
hFileRes := CreateFile(PChar(FileName),
GENERIC_READ or GENERIC_WRITE,
0,
nil,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
Result := (hFileRes = INVALID_HANDLE_VALUE);
if not Result then
CloseHandle(hFileRes);
end;
|
alice
初階會員 發表:41 回覆:49 積分:28 註冊:2002-04-30 發送簡訊給我 |
|
ralph
初階會員 發表:41 回覆:82 積分:29 註冊:2003-02-04 發送簡訊給我 |
There is one function to do this. HFILE OpenFile( LPCSTR lpFileName, // pointer to filename
LPOFSTRUCT lpReOpenBuff, // pointer to buffer for file information
UINT uStyle // action and attributes
); If it returns "HFILE_ERROR", it means " File in use".
|
alice
初階會員 發表:41 回覆:49 積分:28 註冊:2002-04-30 發送簡訊給我 |
function FileLocked(Fn: string): Boolean; var I : Integer; Struct: TOfStruct; Style: Cardinal; Hdl: Hfile; Drive: String; begin Style := OF_Share_Exclusive; //排它方式打開 Drive := UpperCase(Fn[1]); Struct.fFixedDisk := Ord(Drive <> 'A'); //判斷是否是硬碟 Struct.cBytes := SizeOf(Struct); For I := 1 to Length(Fn) do Struct.szPathName[I-1] := Fn[I]; Struct.szPathName[I] := Chr(0); //填充文件名 Hdl := OpenFile(Pchar(Fn), Struct, Style); if Hdl = HFILE_ERROR then begin Result := True; //文件被鎖定 Showmessage(SysErrorMessage(GetLastError)); //顯示錯誤原因 end else Result := False; end;我試過以 if FileLocked('c:\temp\A.Txt') then showmessage('File in Use') else showmessage('File not Use');在執行這段之前, 先以WordPad打開'c:\temp\A.Txt' , 結果是showmessage('File not Use'), 我想大概檔案一定要是鎖定狀態, 才能判斷是否使用中吧!! 若採分享狀態, 其他的程序是無法判斷是否使用中吧??? 還是謝謝 > |
ralph
初階會員 發表:41 回覆:82 積分:29 註冊:2003-02-04 發送簡訊給我 |
|
alice
初階會員 發表:41 回覆:49 積分:28 註冊:2002-04-30 發送簡訊給我 |
|
ralph
初階會員 發表:41 回覆:82 積分:29 註冊:2003-02-04 發送簡訊給我 |
I think the function name "FileLock" confused you. You need to focus on the function "OpenFile". Let's try to say it in Chinese
"This file can be open? No, it is failed.(Result:=false). You mean it is locked(Result:=true)". if Hdl = HFILE_ERROR then begin
Result := True; //文件被鎖定
Showmessage(SysErrorMessage(GetLastError)); //顯示錯誤原因
end else
Result := False;
if Hdl = HFILE_ERROR then begin
Result := false; //The file is failed to open (Can we open the file? No! So, the file is locked? Yes!)
end else
Result := true;
To think the following line:
if (OpenFile(....) <> HFILE_ERROR) then showmessage('...successfully');
or
if (OpenFile(....)) then showmessage('...successfully');
Sorry! I do not have Chinese keyboard on my hand. I hope my explanation in English is clear. 發表人 - ralph 於 2004/02/26 00:11:26
|
william
版主 發表:66 回覆:2535 積分:3048 註冊:2002-07-11 發送簡訊給我 |
引言:我以word叫出文字檔, 可測試出該檔案正被使用中, 再以WordPad叫出文字檔, 則無法測試出該檔案正被使用中, 想再請問, 若文字檔沒被鎖定, 是採分享的方式, 那有何方式, 判斷有人正在使用中???>>< face="Verdana, Arial, Helvetica"> For Notepad and Wordpad, the WHOLE file is being read into the application's memory and closed. You can even delete the file afterwards. So I think the best you can do is detecting whether the file is being opened (i.e. fail in acquiring an exlusing lock) or not. Refering to your original question, please test if your ftp server should hold a lock on the file during transfer (probably yes). Or you could use the file creation/modification datetime for some kind of additional reference. |
ko
資深會員 發表:28 回覆:785 積分:444 註冊:2002-08-14 發送簡訊給我 |
alice 你好:
下面是一篇有關win32檔案異動的訊息得知或許post不完全,若是要詳細資料
可能要請你搜尋一下了
uses
Windows, Messages,shlobj .....
const
SHCNE_RENAMEITEM = $1;
SHCNE_CREATE = $2;
SHCNE_DELETE = $4;
SHCNE_MKDIR = $8;
SHCNE_RMDIR = $10;
SHCNE_MEDIAINSERTED = $20;
SHCNE_MEDIAREMOVED = $40;
SHCNE_DRIVEREMOVED = $80;
SHCNE_DRIVEADD = $100;
SHCNE_NETSHARE = $200;
SHCNE_NETUNSHARE = $400;
SHCNE_ATTRIBUTES = $800;
SHCNE_UPDATEDIR = $1000;
SHCNE_UPDATEITEM = $2000;
SHCNE_SERVERDISCONNECT = $4000;
SHCNE_UPDATEIMAGE = $8000;
SHCNE_DRIVEADDGUI = $10000;
SHCNE_RENAMEFOLDER = $20000;
SHCNE_FREESPACE = $40000;
SHCNE_ASSOCCHANGED = $8000000;
SHCNE_DISKEVENTS = $2381F;
SHCNE_GLOBALEVENTS = $C0581E0;
SHCNE_ALLEVENTS = $7FFFFFFF;
SHCNE_INTERRUPT = $80000000;
SHCNF_IDLIST = 0; // LPITEMIDLIST
SHCNF_PATHA = $1; // path name
SHCNF_PRINTERA = $2; // printer friendly name
SHCNF_DWORD = $3; // DWORD
SHCNF_PATHW = $5; // path name
SHCNF_PRINTERW = $6; // printer friendly name
SHCNF_TYPE = $FF;
SHCNF_FLUSH = $1000;
SHCNF_FLUSHNOWAIT = $2000;
SHCNF_PATH = SHCNF_PATHW;
SHCNF_PRINTER = SHCNF_PRINTERW;
WM_SHNOTIFY = $401;
NOERROR = 0; type
TForm1 = class(TForm)
...
...
..
. type PSHNOTIFYSTRUCT=^SHNOTIFYSTRUCT;
SHNOTIFYSTRUCT = record
dwItem1 : PItemIDList;
dwItem2 : PItemIDList;
end;
Type PSHFileInfoByte=^SHFileInfoByte;
_SHFileInfoByte = record
hIcon :Integer;
iIcon :Integer;
dwAttributes : Integer;
szDisplayName : array [0..259] of char;
szTypeName : array [0..79] of char;
end;
SHFileInfoByte=_SHFileInfoByte;
Type PIDLSTRUCT = ^IDLSTRUCT;
_IDLSTRUCT = record
pidl : PItemIDList;
bWatchSubFolders : Integer;
end;
IDLSTRUCT =_IDLSTRUCT; function SHNotify_Register(hWnd : Integer) : Bool;
function SHNotify_UnRegister:Bool;
//function SHEventName(strPath1,strPath2:string;lParam:Integer):string;
Function SHChangeNotifyDeregister(hNotify:integer):integer;stdcall;
external 'Shell32.dll' index 4;
Function SHChangeNotifyRegister(hWnd,uFlags,dwEventID,uMSG,cItems:LongWord;
lpps:PIDLSTRUCT):integer;stdcall;external 'Shell32.dll' index 2;
Function SHGetFileInfoPidl(pidl : PItemIDList;
dwFileAttributes : Integer;
psfib : PSHFILEINFOBYTE;
cbFileInfo : Integer;
uFlags : Integer):Integer;stdcall;
external 'Shell32.dll' name 'SHGetFileInfoA'; implementation {$R *.dfm} type USER_INFO_3 = packed record
usri3_name : PWideChar;
usri3_password : PWideChar;
usri3_password_age: PDWord;
usri3_priv: PDWord;
usri3_home_dir: PWideChar;
usri3_comment: PWideChar;
usri3_flags: PDWord;
usri3_script_path: PWideChar;
usri3_auth_flags: PDWord;
usri3_full_name: PWideChar;
usri3_usr_comment: PWideChar;
usri3_parms: PWideChar;
usri3_workstations: PWideChar;
usri3_last_logon: PDWord;
usri3_last_logoff: PDWord;
usri3_acct_expires: PDWord;
usri3_max_storage: PDWord;
usri3_units_per_week: PDWord;
usri3_logon_hours: PByte;
usri3_bad_pw_count: PDWord;
usri3_num_logons: PDWord;
usri3_logon_server: PWideChar;
usri3_country_code: PDWord;
usri3_code_page: PDWord;
usri3_user_id: PDWord;
usri3_primary_group_id: PDWord;
usri3_profile: PWideChar;
usri3_home_dir_drive: PWideChar;
usri3_password_expired: PDWord;
end;
type PUSER_INFO_3 = ^USER_INFO_3; function NetUserEnum(ServerName:PWideChar;Level:DWORD;Filter:DWORD;var Buf:Pointer;PrefMaxLen:DWORD;
var EntriesRead:PDWord;var TotalEntries:PDWord;var ResumeHandle:PDWord):Longint;stdcall;external 'netapi32.dll' Name 'NetUserEnum'; function SHEventName(strPath1,strPath2:string;lParam:Integer):string;
var
sEvent:String;
begin
case lParam of
SHCNE_RENAMEITEM: sEvent := '重命名文件' strPath1 '?' strpath2;
SHCNE_CREATE: sEvent := '建立文件 檔案名:' strPath1;
SHCNE_DELETE: sEvent := '刪除文件 檔案名:' strPath1;
SHCNE_MKDIR: sEvent := '新建目錄 目錄名:' strPath1;
SHCNE_RMDIR: sEvent := '刪除目錄 目錄名:' strPath1;
SHCNE_MEDIAINSERTED: sEvent := strPath1 '中插入可移動存儲介質';
SHCNE_MEDIAREMOVED: sEvent := strPath1 '中移去可移動存儲介質' strPath1 ' ' strpath2;
SHCNE_DRIVEREMOVED: sEvent := '移去驅動器' strPath1;
SHCNE_DRIVEADD: sEvent := '添加驅動器' strPath1;
SHCNE_NETSHARE: sEvent := '改變目錄' strPath1 '的共用屬性';
SHCNE_ATTRIBUTES: sEvent := '改變文件目錄屬性 檔案名' strPath1;
SHCNE_UPDATEDIR: sEvent := '更新目錄' strPath1;
SHCNE_UPDATEITEM: sEvent := '更新文件 檔案名:' strPath1;
SHCNE_SERVERDISCONNECT: sEvent := '(斷)伺服器的連接' strPath1 ' ' strpath2;
SHCNE_UPDATEIMAGE: sEvent := 'SHCNE_UPDATEIMAGE';
SHCNE_DRIVEADDGUI: sEvent := 'SHCNE_DRIVEADDGUI';
SHCNE_RENAMEFOLDER: sEvent := '重命名文件夾' strPath1 '?' strpath2;
SHCNE_FREESPACE: sEvent := '磁碟空間大小改變';
SHCNE_ASSOCCHANGED: sEvent := '改變文件關聯';
else
sEvent:='未知操作' IntToStr(lParam);
end;
Result:=sEvent;
end;
}
function SHNotify_Register(hWnd : Integer) : Bool;
var
ps:PIDLSTRUCT;
begin
Result:=False;
new(ps);
If m_hSHNotify = 0 then begin
if SHGetSpecialFolderLocation(0, CSIDL_DESKTOP,
m_pidlDesktop)<> NOERROR then
Form1.close;
if Boolean(m_pidlDesktop) then begin
ps.bWatchSubFolders := 1;
ps.pidl := m_pidlDesktop;
m_hSHNotify := SHChangeNotifyRegister(hWnd, (SHCNF_TYPE Or SHCNF_IDLIST),
(SHCNE_ALLEVENTS Or SHCNE_INTERRUPT),
WM_SHNOTIFY, 1, ps);
Result := Boolean(m_hSHNotify);
end
Else
CoTaskMemFree(m_pidlDesktop);
End;
end; function SHNotify_UnRegister:Bool;
begin
Result:=False;
If Boolean(m_hSHNotify) Then
If Boolean(SHChangeNotifyDeregister(m_hSHNotify)) Then begin
{$R-}
m_hSHNotify := 0;
CoTaskMemFree(m_pidlDesktop);
Result := True;
{$R-}
End;
end;
------
====================== 昏睡~ 不昏睡~ 不由昏睡~ |
alice
初階會員 發表:41 回覆:49 積分:28 註冊:2002-04-30 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |