Return user names from AD FileSystemWatcher

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Im using FileSystemWatcher to monitor a directory. I will eventually have it setup to log everything to a sql database which i can do easily. what i am having a problem with is figuring out how to log the user name of the person who changed or modified a
file? Below is my code

<pre class="prettyprint using System;
using System.IO;
using System.Diagnostics;
using System.Security.Permissions;
using System.Text;

namespace Montor.Directory.Activity
{
public class Watcher
{
public static void Main()
{
Run();
}

[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
public static void Run()
{
string[] args = Environment.GetCommandLineArgs();

//If a directory is not specified exit.
if (args.Length != 2)
{
//Display the way to call.
Console.WriteLine("Usage: Montor.Directory.Activity.exe (Directory)");
return;
}

//Create a new FileSystemWatcher and set its properties
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = args[1];
watcher.IncludeSubdirectories = true;
/* Watch for changes in LastAccess and LastWrite times, and
* the renaming of files or directories. */
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.Security;

//Event Handlers
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.Deleted += new FileSystemEventHandler(OnChanged);
watcher.Renamed += new RenamedEventHandler(OnRenamed);

//Begin Watching
watcher.EnableRaisingEvents = true;

Console.WriteLine("Press q to quit the sample.");
while (Console.Read() != q) ;
}

//Define Event Handlers
private static void OnChanged(object source, FileSystemEventArgs e)
{
// Specify what is done when a file is changed, created, or deleted.
Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
}

private static void OnRenamed(object source, RenamedEventArgs e)
{
// Specify what is done when a file is renamed.
Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath);

}


}
}[/code]
<br/><hr class="sig Devon Quick

View the full article
 

Similar threads

P
Replies
0
Views
116
Priya Bange
P
Back
Top