检查文件是否从其他进程打开 [英] Check if a file is open from another process

查看:133
本文介绍了检查文件是否从其他进程打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

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

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

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

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"

然后来自Powerscript:

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)

这篇关于检查文件是否从其他进程打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆