Check if a file is open from another process

醉酒当歌 提交于 2019-12-04 04:21:07

问题


How do I check if a file is already used by another process from Powerscript ?


回答1:


The best way that I found is to call the WinAPI CreateFile to open a given file in exclusive mode.

First, declare the following Local External Function (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"

then from 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)



回答2:


You could try to open it and if it errors then it is probably already locked.



来源:https://stackoverflow.com/questions/409325/check-if-a-file-is-open-from-another-process

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!