나는이 질문에 대한 답으로 싸우고 있었고 많은 연구를 한 후에 정보를 내 경험과 결합했으며 그 대답은 다음과 같습니다.
//Purpose: Monitor changes to a folder and write to event log when a change
// happens.
//Last Mod date: 09/01/2019
//Author: Bheki
//Notes: The target folder is given in the parameter file. The first time the application runs must be run as an administrator.
using System;
using System.IO;
using System.Security.Permissions;
using System.Diagnostics;
using System.Threading;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Windows;
public class Watcher
{
public static void Main()
{
Run();
}
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
public static void Run()
{
// Create a new FileSystemWatcher and set its properties.
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = Watcher.GetTargetFolder().Item1;
/* 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;
// Only watch text files.
watcher.Filter = Watcher.GetTargetFolder().Item2;
// Add 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;
// Wait for the user to quit the program.
Console.WriteLine("Press \'q\' to quit the sample.");
while (Console.Read() != 'q') ;
}
public static Tuple<string,string> GetTargetFolder()
{
string currentRecordOpenFile;
char ColumnDelimter = ","[0];
string TargetFolder = "";
string FixSelected = "";
string FileExtension = "";
string SecondHeader = "";
TargetFolder = System.IO.Directory.GetCurrentDirectory().Replace("\\", "/");
string ParameterFile = TargetFolder + "/ParameterFile.txt";
using (StreamReader objReader = new StreamReader(ParameterFile.Replace("\\", "/")))
{
while (!objReader.EndOfStream)
{
currentRecordOpenFile = objReader.ReadLine(); //this reads a whole line of input from the file
string[] LineOfFile = currentRecordOpenFile.Split(ColumnDelimter);
if (LineOfFile[0] == "Pathtofiles")
{
//we can skip the heading
continue;
}
else
{
TargetFolder = LineOfFile[0].Replace("\\", "/") + "/"; //this can be changed depending on how the output looks
FixSelected = LineOfFile[1];
FileExtension = LineOfFile[2];
SecondHeader = LineOfFile[3];
}
}
return Tuple.Create(TargetFolder, FileExtension);
}//End of stream for reading the parameter file
}
// Define the 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);
string vLog = "Application";
string vSource = "BCMPR";
if (!EventLog.SourceExists(vSource))
{
//An event log source should not be created and immediately used.
//There is a latency time to enable the source, it should be created
//prior to executing the application that uses the source.
//Execute this sample a second time to use the new source.
EventLog.CreateEventSource(vSource, vLog);
Console.WriteLine("CreatedEventSource");
Console.WriteLine("Exiting, execute the application a second time to use the source.");
// The source is created. Exit the application to allow it to be registered.
return;
}
// Create an EventLog instance and assign its source.
EventLog myLog = new EventLog();
myLog.Source = vSource;
Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
Console.WriteLine("Writting to event log");
// Write an informational entry to the event log.
myLog.WriteEntry("The folder has been changed", EventLogEntryType.Information, 1307);
System.Environment.Exit(0);
}
}
C #으로 빌드 된 후 애플리케이션과 동일한 위치에 있습니다. ParameterFile.txt 파일을 넣고 다음 내용을 포함하십시오.
Pathtofiles,FixSelected,FileExtension,SecondHeader
C:\Data\TEST,ALL,*.txt,TEST
폴더 (C : \ Data \ TEST) 및 파일 확장자 (* .txt)는 사용자 요구 사항으로 변경할 수 있습니다.
// Windows 이벤트 뷰어 및 설정
이 코드가 두 번 실행되고 폴더가 변경되면 Windows 이벤트 뷰어에서 이벤트를 볼 수 있습니다. 이미지의 지시 사항을 따르십시오 :
Windows 이벤트 로그 및 작업 설정