FILESYSTEMWATCHER更新 [英] FILESYSTEMWATCHER UPDATE

查看:49
本文介绍了FILESYSTEMWATCHER更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否将对filesystemwatcher进行更新以获取每个动作(创建,重命名,删除,更改)的用户名?对于每个动作(创建,重命名,删除,更改)?

您是否检查了filesystemwatcher,没有显示任何代码,因此我不知道您的代码是如何编码的,以查看其中是否有任何信息返回该信息?正常 FileSystemWatcher   AFAIK不是为此功能而设计的.但是您没有显示您使用的代码,该代码可能完全不同并且可能是自定义的,因此也许可以提供该功能.

特别是如果下面的代码已从此链接转换为下面的VB.Net- 获取要访问的文件的用户名-可以使用,并且已经在您的代码中.但我相信您需要访问事件日志以获取此类信息.而且我没有测试下面的代码来查看它是否可以满足您的要求.尽管我还有其他代码 访问事件日志以获取此代码下方的信息.不记得为什么我在其中注释掉了代码.图片在代码下方.

事件日志类

公共共享函数DisplayEventAndLogInformation(fileToSearch作为字符串,actionTime作为DateTime)作为EventUnit
Dim sb作为新的StringBuilder()
const queryString As String =< QueryList>"; & vbCr& vbLf& " <查询ID ="0"路径=安全" & vbCr& vbLf& " <选择路径="安全""*"/选择". & vbCr& vbLf& " </Query>" & vbCr& vbLf& </QueryList>>
昏暗的eventQuery作为新的EventLogQuery("Security",PathType.LogName,queryString)
eventsQuery.ReverseDirection = True
将logReader调暗为新的EventLogReader(eventsQuery)
昏暗为新的EventUnit()
Dim isStop As Boolean = False
昏暗的eventInstance作为EventRecord = logReader.ReadEvent()
虽然eventInstance没什么
对于eventInstance.Properties中的每个VARIABLE As var
如果VARIABLE.Value.ToString().ToLower().Contains(fileToSearch.ToLower())并且也actionTime.ToString("d/M/yyyy HH:mm:ss")= eventInstance.TimeCreated.Value.ToString("; d/M/yyyy HH:mm:ss)然后
对于eventInstance.Properties中的每个VARIABLE2作为var
sb.AppendLine(VARIABLE2.Value.ToString())
下一个
e.Message = sb.ToString()
e.User = If((eventInstance.Properties.Count> 1),eventInstance.Properties(1).Value.ToString(),"n/a")
e.File = fileToSearch
isStop =真
退出
万一
下一个
如果isStop然后
退出时
万一
'Console.WriteLine(描述:{0}",eventInstance.FormatDescription());
尝试
异常捕获e2
结束尝试
eventInstance = logReader.ReadEvent()
结束时间
返回e
结束功能

'================================================= ======
'Telerik(www.telerik.com)提供的服务
'由NRefactory支持的转换.
推特:@telerik
'Facebook:facebook.com/telerik
'================================================= ====== 

严格启用选项

公开课表格1

    私有WithEvents mySysLog作为EventLog

    昏暗的TimeIt作为新的秒表()

    私有子Form1_Load(作为对象发送,作为EventArgs发送)处理MyBase.Load
        Me.Text =检索一个日志条目大约花费0.00803348982255139569569174448756843秒".
        Me.CenterToScreen()
        Label1.Text =正在等待"
        Label2.Text =正在等待"
        Label3.Text =等待"
        Label4.Text =正在等待"
        Timer1.Interval = 10
        Timer2.Interval = 500
    结束子

    昏暗以布尔值=假运行

    私有子Button1_Click(作为对象发送,作为EventArgs发送)处理Button1.Click
        Button1.Enabled =假
        TimeIt.Restart()
        Label2.Text =开始时间为" & TimeIt.Elapsed.ToString& ."
        Label3.Text =等待"
        Label4.Text =正在等待"
        RichTextBox1.Text =""
        Temp ="
        计数= 0
        TotalCount = 0
        运行=真
        Timer1.Start()
        BackgroundWorker1.RunWorkerAsync()
    结束子

    Dim Temp As String ="
    昏暗计数为整数= 0
    昏暗的TotalCount为整数= 0

    私有子BackgroundWorker1_DoWork(作为对象发送,作为System.ComponentModel.DoWorkEventArgs发送)处理BackgroundWorker1.DoWork
        mySysLog =新的EventLog()
        'mySysLog.Log ="System";
        mySysLog.Log =安全性";
        TotalCount = mySysLog.Entries.Count
        对于i = 0到mySysLog.Entries.Count-1
            计数= i + 1
            Temp& = mySysLog.Entries.Item(i).Message.ToString& vbCrLf
        下一个
        'mySysLog.EnableRaisingEvents = True
        'AddHandler mySysLog.EntryWritten,AddressOf EntryWritten
        运行=假
    结束子

    私有子Timer1_Tick(作为对象发送,作为EventArgs发送)处理Timer1.Tick
        如果运行= True,则
            Label1.Text =正在检索的系统日志条目是数字". & Count.ToString& "的& TotalCount.ToString& "条目."
        否则如果运行= False,则
            Timer1.Stop()
            Label3.Text =日志搜索结束于" & TimeIt.Elapsed.ToString& ."
            Timer2.Start()'计时器2是必需的,以便在RichTextBox尝试显示Temp之前允许label3有时间更新.
        万一

    结束子

    私有子Timer2_Tick(作为对象发送,作为EventArgs发送)处理Timer2.Tick
        Timer2.Stop()
        My.Computer.FileSystem.WriteAllText("C:\ Users \ John \ Desktop \ SecurityLogInfo.Txt",Temp,False)
        RichTextBox1.Text =临时
        Label1.Text =正在等待"
        Label4.Text ="RichTextBox,显示在"& TimeIt.Elapsed.ToString& "减去约500毫秒."
        TimeIt.Stop()
        Button1.Enabled =真
    结束子

    '目前尚不使用下面的代码,不记得它以前是否起作用.

    Private Sub EntryWritten(ByVal发送者为对象,ByVal e为System.Diagnostics.EntryWrittenEventArgs)
        'Dim sType As String ="

        '选择大小写e.Entry.EntryType
        '案例EventLogEntryType.Information
        'sType =信息"
        '案例EventLogEntryType.Error
        'sType =错误"
        '案例EventLogEntryType.Error
        'sType =警告";
        '结束选择

        '昏暗的我= 0
        '我= e.Entry.InstanceId
        '如果我= 1073748866
        'ListBox1.Items.Add(e.Entry.InstanceId.ToString)
        'ListBox1.Items.Add(e.Entry.Message&"...("& sType&)" e.Entry.InstanceId.ToString)
        'MessageBox.Show(e.Entry.InstanceId.ToString)
        '万一
    结束子

结束班级


Will filesystemwatcher be updated for getting username for each action (created, renamed, deleted, changed)?

解决方案

Will filesystemwatcher be updated for getting username for each action (created, renamed, deleted, changed)?

Did you check your filesystemwatcher, which you didn't display any code for so I don't know how yours is coded, to see if anything in it returns that information? A normal FileSystemWatcher AFAIK isn't designed for that capability. But you didn't show the code yours uses which perhaps is entirely different and may be custom so perhaps it could provide that capability.

Especially if the code below, converted to VB.Net below, from this link - Get Username of an Accesed File - works and is part of your code already. But I believe you need to access an event log for that kind of information. And I didn't test the code below to see if it could do what you want. Although I've other code for accessing event logs for information below this code. Don't remember why I commented code out in it. Image is below the code.

Event Log class

Public Shared Function DisplayEventAndLogInformation(fileToSearch As String, actionTime As DateTime) As EventUnit
	Dim sb As New StringBuilder()
	Const  queryString As String = "<QueryList>" & vbCr & vbLf & "  <Query Id=""0"" Path=""Security"">" & vbCr & vbLf & "    <Select Path=""Security"">*</Select>" & vbCr & vbLf & "  </Query>" & vbCr & vbLf & "</QueryList>"
	Dim eventsQuery As New EventLogQuery("Security", PathType.LogName, queryString)
	eventsQuery.ReverseDirection = True
	Dim logReader As New EventLogReader(eventsQuery)
	Dim e As New EventUnit()
	Dim isStop As Boolean = False
	Dim eventInstance As EventRecord = logReader.ReadEvent()
	While eventInstance IsNot Nothing
		For Each VARIABLE As var In eventInstance.Properties
			If VARIABLE.Value.ToString().ToLower().Contains(fileToSearch.ToLower()) AndAlso actionTime.ToString("d/M/yyyy HH:mm:ss") = eventInstance.TimeCreated.Value.ToString("d/M/yyyy HH:mm:ss") Then
				For Each VARIABLE2 As var In eventInstance.Properties
					sb.AppendLine(VARIABLE2.Value.ToString())
				Next
				e.Message = sb.ToString()
				e.User = If((eventInstance.Properties.Count > 1), eventInstance.Properties(1).Value.ToString(), "n/a")
				e.File = fileToSearch
				isStop = True
				Exit For
			End If
		Next
		If isStop Then
			Exit While
		End If
				'    Console.WriteLine("Description: {0}", eventInstance.FormatDescription());
		Try
		Catch e2 As Exception
		End Try
		eventInstance = logReader.ReadEvent()
	End While
	Return e
End Function

'=======================================================
'Service provided by Telerik (www.telerik.com)
'Conversion powered by NRefactory.
'Twitter: @telerik
'Facebook: facebook.com/telerik
'=======================================================

Option Strict On

Public Class Form1

    Private WithEvents mySysLog As EventLog

    Dim TimeIt As New Stopwatch()

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Text = "It takes about 0.00803348982255139569174448756843 seconds to retrieve one log entry."
        Me.CenterToScreen()
        Label1.Text = "Waiting"
        Label2.Text = "Waiting"
        Label3.Text = "Waiting"
        Label4.Text = "Waiting"
        Timer1.Interval = 10
        Timer2.Interval = 500
    End Sub

    Dim Running As Boolean = False

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Button1.Enabled = False
        TimeIt.Restart()
        Label2.Text = "Start time is " & TimeIt.Elapsed.ToString & "."
        Label3.Text = "Waiting"
        Label4.Text = "Waiting"
        RichTextBox1.Text = ""
        Temp = ""
        Count = 0
        TotalCount = 0
        Running = True
        Timer1.Start()
        BackgroundWorker1.RunWorkerAsync()
    End Sub

    Dim Temp As String = ""
    Dim Count As Integer = 0
    Dim TotalCount As Integer = 0

    Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        mySysLog = New EventLog()
        'mySysLog.Log = "System"
        mySysLog.Log = "Security"
        TotalCount = mySysLog.Entries.Count
        For i = 0 To mySysLog.Entries.Count - 1
            Count = i + 1
            Temp &= mySysLog.Entries.Item(i).Message.ToString & vbCrLf
        Next
        'mySysLog.EnableRaisingEvents = True
        'AddHandler mySysLog.EntryWritten, AddressOf EntryWritten
        Running = False
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        If Running = True Then
            Label1.Text = "System log entry being retrieved is number " & Count.ToString & " of " & TotalCount.ToString & " entries."
        ElseIf Running = False Then
            Timer1.Stop()
            Label3.Text = "Log search finished at " & TimeIt.Elapsed.ToString & "."
            Timer2.Start() ' Timer 2 was necessary to allow label3 time to update before the RichTextBox attemps to display Temp.
        End If

    End Sub

    Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
        Timer2.Stop()
        My.Computer.FileSystem.WriteAllText("C:\Users\John\Desktop\SecurityLogInfo.Txt", Temp, False)
        RichTextBox1.Text = Temp
        Label1.Text = "Waiting"
        Label4.Text = "RichTextBox displaying text finished at " & TimeIt.Elapsed.ToString & " minus about 500 miliseconds."
        TimeIt.Stop()
        Button1.Enabled = True
    End Sub

    ' The below code is not being used at this time, don't remember if it worked before.

    Private Sub EntryWritten(ByVal sender As Object, ByVal e As System.Diagnostics.EntryWrittenEventArgs)
        'Dim sType As String = ""

        'Select Case e.Entry.EntryType
        'Case EventLogEntryType.Information
        'sType = "Information"
        'Case EventLogEntryType.Error
        'sType = "Error"
        'Case EventLogEntryType.Error
        'sType = "Warning"
        'End Select

        'Dim i As Long = 0
        ' i = e.Entry.InstanceId
        'If i = 1073748866 Then
        'ListBox1.Items.Add(e.Entry.InstanceId.ToString)
        'ListBox1.Items.Add(e.Entry.Message & "...(" & sType & ") " & e.Entry.InstanceId.ToString)
        'MessageBox.Show(e.Entry.InstanceId.ToString)
        'End If
    End Sub

End Class


这篇关于FILESYSTEMWATCHER更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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