服务vb.net中的MS Word文件名和时间计数器

时间:2017-12-13 05:38:32

标签: .net vb.net process windows-services

我正在提供服务来跟踪文件何时打开和关闭。打开时我有文件名,并将该名称添加到列表中。但是,当我关闭一个word文件WMI通知文件已关闭时,我无法获取已关闭的文件名。

单词文件打开或关闭时的管理事件观察器跟踪器。 Process.GetProcessByName("winword")获取开放流程的名称

管理事件观察员:

 For Each a In str

  startstr += "processname = '" + a + "'" + " OR "
   Next
    startstr = startstr.Substring(0, startstr.Length - 4)
        qrstart = "SELECT * FROM Win32_ProcessStartTrace WITHIN 1 WHERE " + startstr + ""
        qrstop = "SELECT * FROM Win32_ProcessStopTrace WITHIN 1 WHERE " + startstr + ""


            startWatch = New ManagementEventWatcher(New WqlEventQuery(qrstart))

            AddHandler startWatch.EventArrived, _
                AddressOf startWatch_EventArrived
            Try

                startWatch.Start()
            Catch ex As Exception
                Console.WriteLine("Ex {0} ", ex.ToString())
            End Try

            stopWatch = New ManagementEventWatcher(New WqlEventQuery(qrstop))
            AddHandler stopWatch.EventArrived, _
                AddressOf stopWatch_EventArrived
            Try
                stopWatch.Start()
            Catch ex As Exception
                Console.WriteLine("Ex {0} ", ex.ToString())
            End Try


            FileTxt.Close()
Private Sub startWatch_EventArrived(ByVal sender As Object, ByVal e As EventArrivedEventArgs)
        Dim FileTxt As System.IO.StreamWriter
        FileTxt = My.Computer.FileSystem.OpenTextFileWriter("E:\text.txt", True)
        FileTxt.WriteLine("Process started: {0} at Time {1}", e.NewEvent.Properties("ProcessName").Value, Now)
        'FileTxt.WriteLine("Time of start : {0}", Now)
        FileTxt.WriteLine()
        FileTxt.Close()

    End Sub

0 个答案:

没有答案