我以前做了个程序 Private Declare Function FindWindow Lib "user32" Alias _ "FindWindowA" (ByVal lpClassName As String, _ ByVal lpWindowName As String) As Long Private Declare Function PostMessage Lib "user32" Alias _ "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _ ByVal wParam As Long, lParam As Any) As Long Dim winHwnd As Long按钮检测文件打没打开 Private Sub Command2_Click() winHwnd = FindWindow(vbNullString, "Flash") Debug.Print winHwnd If winHwnd <> 0 Then Label3.Caption = 1 Command3.Enabled = 1 Else Label3.Caption = 0 MsgBox ("文件不存在") Command3.Enabled = 0 End If end sub
用KILL都会错吗? Private Sub Command1_Click() On Error GoTo acd Kill "f:\abc\c.txt" Exit Sub acd: MsgBox "已经打开" End Sub
Private Sub Command1_Click() dim ChackFileOF as string On Error GoTo err3 ChackFileOF ="f:\abc\c.xls" If Dir(ChackFileOF) Then Kill ChackFileOF Else '在这里 我写的是 拷贝一个新文件,取名为 d.xls,然后但开 d.xls End If err3: '错误处理 MsgBox "文件已打开!", vbExclamation, "提示" End Sub 这样 On Error GoTo 语句都会报错呀
看看msdn,有isexist判断文件和文件架是否存在的函数
Private Sub Command1_Click() Dim ChackFileOF As String Dim fs As New FileSystemObject On Error GoTo err3 ChackFileOF = "d:\c.xls" If Dir(ChackFileOF) <> "" Then Kill ChackFileOF Else fs.CopyFile "d:\jjj\book1.xls", "d:\kof.xls", True End End If err3: '错误处理 MsgBox "文件已打开!", vbExclamation, "提示" End Sub
function 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;
procedure TForm1.FormCreate(Sender: TObject); var bool:boolean; function 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; begin bool:=isfileinuse('C:\windows\system32\mmc.exe'); if bool then showmessage('in useing') else showmessage('not in useing'); end; ============================= 抄的,也不是很好用,Delphi的。
--//判断一个文件是否存在和是否打开 先引用:microsoft scripting runtime 如:D盘的上的aa.txt文件 --- --- Private Sub Command1_Click() Dim hwndcalc As Integer Dim fso As New FileSystemObject If fso.FileExists("d:\aa.txt") = True Then MsgBox "此文件存在!" Else MsgBox "此文件不存在!" Exit Sub End If On Error GoTo err: fso.CopyFile "d:\aa.txt", "e:\aa.txt", True MsgBox "打开了!" Exit Sub err: MsgBox "没打开!" End Sub
Private Declare Function FindWindow Lib "user32" Alias _
"FindWindowA" (ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Private Declare Function PostMessage Lib "user32" Alias _
"PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, lParam As Any) As Long
Dim winHwnd As Long按钮检测文件打没打开
Private Sub Command2_Click()
winHwnd = FindWindow(vbNullString, "Flash")
Debug.Print winHwnd
If winHwnd <> 0 Then
Label3.Caption = 1
Command3.Enabled = 1
Else
Label3.Caption = 0
MsgBox ("文件不存在")
Command3.Enabled = 0
End If
end sub
里面的“flash”是打开文件的文件头!
所以 需要高手指点。
Private Sub Command1_Click()
On Error GoTo acd
Kill "f:\abc\c.txt"
Exit Sub
acd:
MsgBox "已经打开"
End Sub
dim ChackFileOF as string
On Error GoTo err3
ChackFileOF ="f:\abc\c.xls"
If Dir(ChackFileOF) Then
Kill ChackFileOF
Else
'在这里 我写的是 拷贝一个新文件,取名为 d.xls,然后但开 d.xls
End If
err3: '错误处理
MsgBox "文件已打开!", vbExclamation, "提示"
End Sub
这样 On Error GoTo 语句都会报错呀
Private Sub Command1_Click()
Dim ChackFileOF As String
Dim fs As New FileSystemObject
On Error GoTo err3
ChackFileOF = "d:\c.xls"
If Dir(ChackFileOF) <> "" Then
Kill ChackFileOF
Else
fs.CopyFile "d:\jjj\book1.xls", "d:\kof.xls", True
End
End If
err3: '错误处理
MsgBox "文件已打开!", vbExclamation, "提示"
End Sub
提供了fileExist()
检查文件是否存在
描述如果指定的文件存在,返回 True,若不存在,则返回 False。语法object.FileExists(filespec) FileExists 方法语法有如下几部分:部分 描述
object 必需的。始终是一个 FileSystemObject 的名字。
filespec 必需的。要确定是否存在的文件的名字。如果认为文件不在当前文件夹中,必须提供一个完整的路径说明(绝对的或相对的)。
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;
结合SDK中对CreateFile函数的定义,该函数的似乎是可以实现判断文件在被使用的功能的,然而却没有。有没有其他方法
---------------------------------------------------------------
以可写方式打开文件,如果抱错则正在使用
---------------------------------------------------------------
这个函数完全可以判断出程序是否正被使用,测了一下
procedure TForm1.FormCreate(Sender: TObject);
var
bool:boolean;
function 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;
begin
bool:=isfileinuse('C:\windows\system32\mmc.exe');
if bool then
showmessage('in useing')
else
showmessage('not in useing');
end;
=============================
抄的,也不是很好用,Delphi的。
先引用:microsoft scripting runtime
如:D盘的上的aa.txt文件
---
---
Private Sub Command1_Click()
Dim hwndcalc As Integer
Dim fso As New FileSystemObject
If fso.FileExists("d:\aa.txt") = True Then
MsgBox "此文件存在!"
Else
MsgBox "此文件不存在!"
Exit Sub
End If
On Error GoTo err:
fso.CopyFile "d:\aa.txt", "e:\aa.txt", True
MsgBox "打开了!"
Exit Sub
err:
MsgBox "没打开!"
End Sub