[COLOR=#000000]namespace [/COLOR][COLOR=#2B91AF]FileSystemWatcherTest2[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#000000]{[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#00008B]public[/COLOR][COLOR=#00008B]partial[/COLOR][COLOR=#00008B]class[/COLOR][COLOR=#2B91AF]Form1[/COLOR][COLOR=#000000]:[/COLOR][COLOR=#2B91AF]Form[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#000000]{[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#00008B]public[/COLOR][COLOR=#00008B]static[/COLOR][COLOR=#2B91AF]int[/COLOR][COLOR=#000000] appId[/COLOR][COLOR=#000000];[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#00008B]public[/COLOR][COLOR=#00008B]static[/COLOR][COLOR=#2B91AF]FileSystemWatcher[/COLOR][COLOR=#000000] fs[/COLOR][COLOR=#000000];[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#00008B]public[/COLOR][COLOR=#2B91AF]Form1[/COLOR][COLOR=#000000]()[/COLOR][COLOR=#000000]{[/COLOR][COLOR=#2B91AF]InitializeComponent[/COLOR][COLOR=#000000]();[/COLOR][COLOR=#000000]}[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#00008B]private[/COLOR][COLOR=#00008B]void[/COLOR][COLOR=#2B91AF]Form1_Load[/COLOR][COLOR=#000000]([/COLOR][COLOR=#00008B]object[/COLOR][COLOR=#000000] sender[/COLOR][COLOR=#000000],[/COLOR][COLOR=#2B91AF]EventArgs[/COLOR][COLOR=#000000] e[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#000000]{[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#2B91AF]Random[/COLOR][COLOR=#000000] rnd [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#00008B]new[/COLOR][COLOR=#2B91AF]Random[/COLOR][COLOR=#000000]([/COLOR][COLOR=#2B91AF]DateTime[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]Now[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]Second[/COLOR][COLOR=#000000]);[/COLOR][COLOR=#000000]
appId [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] rnd[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]Next[/COLOR][COLOR=#000000]([/COLOR][COLOR=#800000]1[/COLOR][COLOR=#000000],[/COLOR][COLOR=#800000]9999[/COLOR][COLOR=#000000]);[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#00008B]string[/COLOR][COLOR=#000000] path [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#800000]@"\\IP.Address.here\drive\test.txt"[/COLOR][COLOR=#000000];[/COLOR][COLOR=#000000]
fs [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#00008B]new[/COLOR][COLOR=#2B91AF]FileSystemWatcher[/COLOR][COLOR=#000000]([/COLOR][COLOR=#800000]@"\\IP.Address.here\drive\"[/COLOR][COLOR=#000000],[/COLOR][COLOR=#800000]"*.txt"[/COLOR][COLOR=#000000]);[/COLOR][COLOR=#000000]
fs[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]EnableRaisingEvents[/COLOR][COLOR=#000000]=[/COLOR][COLOR=#00008B]true[/COLOR][COLOR=#000000];[/COLOR][COLOR=#000000]
fs[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]IncludeSubdirectories[/COLOR][COLOR=#000000]=[/COLOR][COLOR=#00008B]false[/COLOR][COLOR=#000000];[/COLOR][COLOR=#000000]
fs[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]NotifyFilter[/COLOR][COLOR=#000000]=[/COLOR][COLOR=#2B91AF]NotifyFilters[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]LastWrite[/COLOR][COLOR=#000000];[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#808080]/* NotifyFilters.Attributes | NotifyFilters.CreationTime
| NotifyFilters.DirectoryName | NotifyFilters.FileName
| NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.Security | NotifyFilters.Size;*/[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#00008B]if[/COLOR][COLOR=#000000](![/COLOR][COLOR=#2B91AF]File[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]Exists[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]path[/COLOR][COLOR=#000000]))[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#000000]{[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#2B91AF]File[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]Create[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]path[/COLOR][COLOR=#000000]);[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#808080]// set full R/W permissions for everyone[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#2B91AF]DirectoryInfo[/COLOR][COLOR=#000000] dInfo [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#00008B]new[/COLOR][COLOR=#2B91AF]DirectoryInfo[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]path[/COLOR][COLOR=#000000]);[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#2B91AF]DirectorySecurity[/COLOR][COLOR=#000000] dSecurity [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] dInfo[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]GetAccessControl[/COLOR][COLOR=#000000]();[/COLOR][COLOR=#000000]
dSecurity[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]AddAccessRule[/COLOR][COLOR=#000000]([/COLOR][COLOR=#00008B]new[/COLOR][COLOR=#2B91AF]FileSystemAccessRule[/COLOR][COLOR=#000000]([/COLOR][COLOR=#00008B]new[/COLOR][COLOR=#2B91AF]SecurityIdentifier[/COLOR][COLOR=#000000]([/COLOR][COLOR=#2B91AF]WellKnownSidType[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]WorldSid[/COLOR][COLOR=#000000],[/COLOR][COLOR=#00008B]null[/COLOR][COLOR=#000000]),[/COLOR][COLOR=#2B91AF]FileSystemRights[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]FullControl[/COLOR][COLOR=#000000],[/COLOR][COLOR=#2B91AF]InheritanceFlags[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]ObjectInherit[/COLOR][COLOR=#000000]|[/COLOR][COLOR=#2B91AF]InheritanceFlags[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]ContainerInherit[/COLOR][COLOR=#000000],[/COLOR][COLOR=#2B91AF]PropagationFlags[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]NoPropagateInherit[/COLOR][COLOR=#000000],[/COLOR][COLOR=#2B91AF]AccessControlType[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]Allow[/COLOR][COLOR=#000000]));[/COLOR][COLOR=#000000]
dInfo[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]SetAccessControl[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]dSecurity[/COLOR][COLOR=#000000]);[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#000000]}[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#808080]// write text to file to raise fs_changed event[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#2B91AF]StreamWriter[/COLOR][COLOR=#000000] sw [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#00008B]new[/COLOR][COLOR=#2B91AF]StreamWriter[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]path[/COLOR][COLOR=#000000],[/COLOR][COLOR=#00008B]true[/COLOR][COLOR=#000000]);[/COLOR][COLOR=#000000]
sw[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]WriteLine[/COLOR][COLOR=#000000]([/COLOR][COLOR=#2B91AF]DateTime[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]Now[/COLOR][COLOR=#000000]+[/COLOR][COLOR=#800000]" | App Id: "[/COLOR][COLOR=#000000]+[/COLOR][COLOR=#000000] appId [/COLOR][COLOR=#000000]+[/COLOR][COLOR=#800000]" | File changed..."[/COLOR][COLOR=#000000]);[/COLOR][COLOR=#000000]
sw[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]Close[/COLOR][COLOR=#000000]();[/COLOR][COLOR=#000000]
sw[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]Dispose[/COLOR][COLOR=#000000]();[/COLOR][COLOR=#000000]
GC[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]Collect[/COLOR][COLOR=#000000]();[/COLOR][COLOR=#808080]// just in case... [/COLOR][COLOR=#000000]
fs[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]Changed[/COLOR][COLOR=#000000]+=[/COLOR][COLOR=#000000] fs_Changed[/COLOR][COLOR=#000000];[/COLOR][COLOR=#808080]// raise event only for the next instance of this app[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#000000]}[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#00008B]private[/COLOR][COLOR=#00008B]static[/COLOR][COLOR=#00008B]void[/COLOR][COLOR=#000000] fs_Changed[/COLOR][COLOR=#000000]([/COLOR][COLOR=#00008B]object[/COLOR][COLOR=#000000] sender[/COLOR][COLOR=#000000],[/COLOR][COLOR=#2B91AF]FileSystemEventArgs[/COLOR][COLOR=#000000] e[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#000000]{[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#2B91AF]MessageBox[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]Show[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]appId [/COLOR][COLOR=#000000]+ [/COLOR][COLOR=#800000]" | file changed!"[/COLOR][COLOR=#000000]);[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#000000]}[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#000000]}[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#000000]}[/COLOR]