vfp如何创建windows服务?上次没说清,再发一帖
我想用vfp写一个windows服务,监视一个文件夹,如文件夹内文件增加,将新增的文件复制另一个文件夹内。不知道用vfp9.0和win7或winxp如何实现监视一个文件夹,如文件夹内文件增加,将新增的文件复制另一个文件夹内??我用VFP命令复制文件,调试正确,作为服务运行时没反应,是不是VFP命令在windows服务中不能用?请各位大侠指教,如何实现这个功能。最好有代码,谢谢!
[解决办法]
本帖最后由 dkfdtf 于 2013-11-09 23:39:30 编辑 再一次的回答:
http://bbs.csdn.net/topics/390636724
[解决办法]
我不用 Windows 服务响应事件
因为防火墙或杀软或干掉这些服务
还有如果发生了事件
很难及时回馈到 VFP 的窗口中
我采用 FOCUS.FLL 动态库监测目录或文件的事件
很及时
不怎么耗资源
Set Library To FOCUS.FLL
Clear
Local nHandle
If ( NOT_HandlesFree() > 0 )
NOT_Frequency( 1000 ) && 间隔 1 秒内将报告该目录的任何变化。
nHandle = NOT_Set( "C:\Temp\aaa", "WarnMe", .T., 24 )
If ( nHandle != -1 )
? NOT_info( nHandle,1 ) && "C:\Temp\aaa"
? NOT_info( nHandle,2 ) && "WarnMe"
? NOT_info( nHandle,3 ) && .T.
? NOT_info( nHandle,4 ) && 24
Else
=Messagebox( "无法设置通知", 48, "通知" )
Endif
Else
=Messagebox( "无法设置通知,可能不存在 [C:\Temp\aaa] 目录。请释放某些句柄。", 48, "通知" )
Endif
Procedure WarnMe( nHandle ) && 发出警告
=Messagebox( "有人在动我的 C 盘", 64, "通知" )
Return
* 监控指定文件夹中的文件增删状态
Public oForm
Clear
* oForm = CREATEOBJECT("Tform", SYS(2023)) && 监控 TEMP 目录
oForm = Createobject("Tform", "C:\Temp") && 监控 C:\Temp 目录
oForm.Visible = .T.
* 结束主程序
Define Class Tform As Form
#Define FILE_NOTIFY_CHANGE_FILE_NAME 1
#Define FILE_NOTIFY_CHANGE_DIR_NAME 2
#Define FILE_NOTIFY_CHANGE_ATTRIBUTES 4
#Define FILE_NOTIFY_CHANGE_SIZE 8
#Define FILE_NOTIFY_CHANGE_LAST_WRITE 16
#Define FILE_NOTIFY_CHANGE_LAST_ACCESS 32
#Define FILE_NOTIFY_CHANGE_CREATION 64
#Define FILE_NOTIFY_CHANGE_SECURITY 128
#Define INVALID_HANDLE_VALUE -1
#Define WAIT_OBJECT_0 0
#Define WATCHING_INTERVAL 1000 && 间隔时间,单位:毫秒
Protected hNotify, PathBeingWatched
Width = 400
Height = 150
MaxButton = .F.
BorderStyle = 2
AutoCenter = .T.
Caption = "正在监控目录"
hNotify = INVALID_HANDLE_VALUE
PathBeingWatched = ""
Add Object lblTarget As Label With Left = 10, Top = 7, AutoSize = .T.
Add Object tm As Timer With Interval = 0
Add Object lblAlert As Label With Left = 10, Top = 30, ;
Autosize = .T., Caption = "通知 : "
Function Init(cPath)
This.Declare
This.PathBeingWatched = Fullpath(m.cPath)
If Not This.StartWatching()
= Messagebox("通知句柄错误,可能不存在该目录。")
Return .F.
Endif
Procedure Destroy
This.StopWatching
Protected Function StartWatching
Local lResult
* 监控的目录不存在
This.hNotify = FindFirstChangeNotification(;
THIS.PathBeingWatched, 0,;
FILE_NOTIFY_CHANGE_FILE_NAME +;
FILE_NOTIFY_CHANGE_LAST_WRITE )
lResult = (This.hNotify <> INVALID_HANDLE_VALUE)
If lResult
This.lblTarget.Caption = "监控目录 : " +;
THIS.PathBeingWatched
This.tm.Interval = WATCHING_INTERVAL
Endif
Return lResult
Protected Procedure ContinueWatching
If FindNextChangeNotification(This.hNotify) = 0
This.StopWatching
= Messagebox("响应错误.")
This.Release
Endif
This.tm.Interval = WATCHING_INTERVAL
Protected Function StopWatching
This.tm.Interval = 0
If This.hNotify <> INVALID_HANDLE_VALUE
= FindCloseChangeNotification(This.hNotify)
Endif
Function _signaled && 返回信号状态
Return (WaitForSingleObject(This.hNotify, 0) = WAIT_OBJECT_0)
Procedure _notify && 触发事件
Local cMessage
cMessage = "通知 : " + Ttoc(Datetime()) + " 该目录的文件有增删!"
This.lblAlert.Caption = cMessage
Activate Screen
? cMessage
This.ContinueWatching
Procedure tm.Timer
If Thisform._signaled()
Thisform._notify
Endif
Protected Procedure Declare
Declare SHORT FindNextChangeNotification In kernel32;
INTEGER hChangeHandle
Declare SHORT FindCloseChangeNotification In kernel32;
INTEGER hChangeHandle
Declare Integer FindFirstChangeNotification In kernel32;
STRING lpPathName, Integer bWatchSubtree,;
INTEGER dwNotifyFilter
Declare Integer WaitForSingleObject In kernel32;
INTEGER hHandle, Integer dwMilliseconds
Enddefine
If Lower(JustPath(lcPath))='d:\back'
Copy File (lcPath) To 'e:\bak'
Endif
If Lower(JustPath(lcPath))='d:\back'
*Erase 'e:\bak\*.*'&& Copy 前先清空
Copy File 'd:\back\*.*' To 'e:\bak'
Endif