Get Username of an Accesed File

后端 未结 1 1289
情话喂你
情话喂你 2020-12-22 09:24

I would like to get the username of an accessed file (add, delete, rename,...). actually I use filesystemwatcher to monitor the file access and I have activated object acces

相关标签:
1条回答
  • 2020-12-22 10:01

    From My last program ( 2 week ago) - I was asked to audit change in files ( also the user name)

    the Solution was by filesystemwatcher and after an event -> goto the Event Log of windows and bu Xpath search - To find which user made the action.

       public static EventUnit DisplayEventAndLogInformation(string fileToSearch, DateTime actionTime)
            {
                StringBuilder sb = new StringBuilder();
                const string queryString = @"<QueryList>
      <Query Id=""0"" Path=""Security"">
        <Select Path=""Security"">*</Select>
      </Query>
    </QueryList>";
                EventLogQuery eventsQuery = new EventLogQuery("Security", PathType.LogName, queryString);
                eventsQuery.ReverseDirection = true;
                EventLogReader logReader = new EventLogReader(eventsQuery);
                EventUnit e = new EventUnit();
                bool isStop = false;
                for (EventRecord eventInstance = logReader.ReadEvent(); null != eventInstance; eventInstance = logReader.ReadEvent())
                {
                    foreach (var VARIABLE in eventInstance.Properties)
                        if (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"))
                        {
                            foreach (var VARIABLE2 in eventInstance.Properties) sb.AppendLine(VARIABLE2.Value.ToString());
                            e.Message = sb.ToString();
                            e.User = (eventInstance.Properties.Count > 1) ? eventInstance.Properties[1].Value.ToString() : "n/a";
                            e.File = fileToSearch;
                            isStop = true;
                            break;
                        }
                    if (isStop) break;
                    try
                    {
                        //    Console.WriteLine("Description: {0}", eventInstance.FormatDescription());
                    }
                    catch (Exception e2)
                    {
                    }
                }
                return e;
            }
    
    0 讨论(0)
提交回复
热议问题