检查文件是否从其他进程打开

时间:2009-01-03 16:36:36

标签: file process io powerbuilder

如何检查文件是否已被Powerscript的其他进程使用?

2 个答案:

答案 0 :(得分:4)

我找到的最好方法是调用WinAPI CreateFile以独占模式打开给定文件。

首先,声明以下本地外部函数(PB10)

FUNCTION Long CreateFile(ref string lpszName, long fdwAccess, long fdwShareMode, long lpsa, &
long fdwCreate, long fdwAttrsAndFlags, long hTemplateFile) LIBRARY "Kernel32.dll" &
ALIAS FOR "CreateFileA;Ansi"
FUNCTION boolean CloseHandle (long file_hand) LIBRARY "KERNEL32.DLL"

然后来自Powerscript:

CONSTANT ulong GENERIC_ACCESS = 268435456  //  &H10000000
CONSTANT ulong EXCLUSIVE_ACCESS = 0
CONSTANT ulong OPEN_EXISTING = 3

long ll_handle
String ls_file 

ls_file = "c:\temp\myfile.xls"

ll_handle = CreateFile ( ls_file, GENERIC_ACCESS, EXCLUSIVE_ACCESS,  0, OPEN_EXISTING, 0, 0) 
IF ll_handle < 1 THEN 
    MessageBox("", "Can't open, maybe missing or already opened ?!?")
ELSE
    MessageBox("","File can be opened")
END IF

CloseHandle(ll_handle)

答案 1 :(得分:0)

您可以尝试打开它,如果错误则可能已经锁定。