Filesystemwatcher doesn't trigger event - c#

I am trying to monitor a directory for new files in it with the FileSystemWatcher Class.
My Problem is that the event doesn't get triggered. My watcher class is:
public class Filewatcher
{
private FileSystemWatcher watcher;
public Filewatcher(string path, string filefilter)
{
this.watcher = new FileSystemWatcher();
this.watcher.Path = path;
this.watcher.NotifyFilter = NotifyFilters.FileName; //| NotifyFilters.LastWrite | NotifyFilters.LastAccess
this.watcher.Filter = filefilter;
this.watcher.Created += new FileSystemEventHandler(OnChanged);
}
public void start_watching()
{
//this.watcher.IncludeSubdirectories = true;
this.watcher.EnableRaisingEvents = true;
Console.WriteLine("Press Enter to quit\r\n");
Console.ReadLine();
}
private static void OnChanged(object source, FileSystemEventArgs e)
{
//FileInfo objFileInfo = new FileInfo(e.FullPath);
//if (!objFileInfo.Exists) return;
Console.WriteLine("got the change\r\n");
global_functions.perform_action(Global_Vars.thereader.read(e.FullPath), Global_Vars.thepattern);
}
}
Operating System is Win 7 Prof x64

You don't make a call to start_watching(). Add a call to that and it may work better.

Related

C# how to repeat function

I am reading directories and adding .ini files to listbox1 or lisbox2 depending what is inside.
private void Form1_Load(object sender, EventArgs e)
{
string rootdir = #"C:\Users\isaced1\Desktop\test"; //root directory of all projects
string[] files = Directory.GetFiles(rootdir, "*.ini", SearchOption.AllDirectories); //searches for specific .ini files in all directories whithin rood directory
//cycles through all .ini files and adds it to lsitbox1 or listbox2
foreach (string item in files)
{
string fileContents = File.ReadAllText(item); //reads all .ini files
const string PATTERN = #"OTPM = true"; //search pattern in .ini files
Match match = Regex.Match(fileContents, PATTERN, RegexOptions.IgnoreCase); //matches pattern with content in .ini file
if (match.Success)
{
listBox1.Items.Add(item); //if match is successfull places file in lisbox1
listBox1.ForeColor = Color.Green;
}
else
{
listBox2.Items.Add(item); //if match is unsuccessfull places file in lisbox2
listBox2.ForeColor = Color.Red;
}
}
}
Now I want to to continuously repeat the process. I tried to do timer, but I keep getting error "error CS0120: An object reference is required for the non-static field, method, or property". I understand that my private void Form1_Load has to be static, but I just dont know way around it.
My timer code
private static System.Timers.Timer aTimer;
public static void Main1()
{
// Create a timer and set a two second interval.
aTimer = new System.Timers.Timer();
aTimer.Interval = 1000;
// Hook up the Elapsed event for the timer.
aTimer.Elapsed += new ElapsedEventHandler(Form1_Load);
// Have the timer fire repeated events (true is the default)
aTimer.AutoReset = true;
// Start the timer
aTimer.Enabled = true;
}
Any suggestions, thanks.
Don't poll for new files (using a timer is inefficient), use events from the OS to tell you when files are created/updated/deleted etc.
https://learn.microsoft.com/en-us/dotnet/api/system.io.filesystemwatcher?view=net-6.0
static void Main()
{
using var watcher = new FileSystemWatcher(#"C:\path\to\folder");
watcher.NotifyFilter = NotifyFilters.Attributes
| NotifyFilters.CreationTime
| NotifyFilters.DirectoryName
| NotifyFilters.FileName
| NotifyFilters.LastAccess
| NotifyFilters.LastWrite
| NotifyFilters.Security
| NotifyFilters.Size;
watcher.Changed += OnChanged;
watcher.Created += OnCreated;
watcher.Deleted += OnDeleted;
watcher.Renamed += OnRenamed;
watcher.Error += OnError;
watcher.Filter = "*.txt";
watcher.IncludeSubdirectories = true;
watcher.EnableRaisingEvents = true;
Console.WriteLine("Press enter to exit.");
Console.ReadLine();
}
private static void OnChanged(object sender, FileSystemEventArgs e)
{
if (e.ChangeType != WatcherChangeTypes.Changed)
{
return;
}
Console.WriteLine($"Changed: {e.FullPath}");
}
private static void OnCreated(object sender, FileSystemEventArgs e)
{
string value = $"Created: {e.FullPath}";
Console.WriteLine(value);
}
private static void OnDeleted(object sender, FileSystemEventArgs e) =>
Console.WriteLine($"Deleted: {e.FullPath}");
private static void OnRenamed(object sender, RenamedEventArgs e)
{
Console.WriteLine($"Renamed:");
Console.WriteLine($" Old: {e.OldFullPath}");
Console.WriteLine($" New: {e.FullPath}");
}
private static void OnError(object sender, ErrorEventArgs e) =>
PrintException(e.GetException());
private static void PrintException(Exception? ex)
{
if (ex != null)
{
Console.WriteLine($"Message: {ex.Message}");
Console.WriteLine("Stacktrace:");
Console.WriteLine(ex.StackTrace);
Console.WriteLine();
PrintException(ex.InnerException);
}
}

Create multiple instances of the same FileSystemWatcher

My program needs to monitor multiple locations, but trigger the same code for each location. As a single FileSystemWatcher can't monitor multiple locations, but is it possible to create multiple instances of it and pass in a folder path for each?
I can't hard code each FileSystemWatcher as more and more locations will need to be added in time and this needs to be done by the end users, as it is highly impractical for me to have to manually hard code a new FileSystemWatcher each time. So my plan was to have the folder paths saved to a file and the program just creates a FileSystemWatcher for each path in the list. But I have no idea if this is possible in the slightest.
Going on the Factory Method Pattern suggestion here the attempt:
I get the errors: "'List' does not contain a definition for 'add'
public void StartWatchers()
{
string[] ArrayPaths = new string[2];
List<FileSystemWatcher> watchers = new List<FileSystemWatcher>();
ArrayPaths[0] = #"K:\Daily Record Checker\Test\Test1";
ArrayPaths[1] = #"K:\Daily Record Checker\Test\Test2";
int i = 0;
foreach (String String in ArrayPaths)
{
watcher.add(MyWatcherFatory(ArrayPaths[i]));
i++;
}
//Do other stuff....
//....
//Start my watchers...
foreach (FileSystemWatcher watcher in watchers)
{
Watcher.EnableRaisingEvents = true;
i++;
}
}
FileSystemWatcher MyWatcherFatory(string path)
{
FileSystemWatcher watcher = new FileSystemWatcher(path);
watcher.Changed += Watcher_Created;
watcher.Path = path;
watcher.Filter = "*.csv";
return watcher;
}
private void Watcher_Created(object sender, FileSystemEventArgs e)
{
System.Threading.Thread.Sleep(1000);
FileInfo fileInfo = new FileInfo(e.FullPath);
if (!IsFileLocked(fileInfo))
{
CheckNumberOfRecordsInFile(e.FullPath);
}
}
Use the factory method pattern.
FileSystemWatcher MyWatcherFatory(string path, object additionalParameters)
{
FileSystemWatcher watcher = new FileSystemWatcher(path);
watcher.Changed += myWatcherChangedMethod;//Attach them to the same listeners,
//Set additional parameters...
return watcher.
}
EDIT: Based on the information you further provided:
public void StartWatchers()
{
string[] ArrayPaths = new string[2];
List<FileSystemWatcher> watchers = new List<FileSystemWatcher>();
ArrayPaths[0] = #"K:\Daily Record Checker\Test\Test1";
ArrayPaths[1] = #"K:\Daily Record Checker\Test\Test2";
int i = 0;
foreach (String String in ArrayPaths)
{
watchers.Add(MyWatcherFatory(ArrayPaths[i]));
i++;
}
//Do other stuff....
//....
//Start my watchers...
foreach (FileSystemWatcher watcher in watchers )
{
watcher.EnableRaisingEvents = true;;
i++;
}
}
FileSystemWatcher MyWatcherFatory(string path)
{
FileSystemWatcher watcher = new FileSystemWatcher(path);
watcher.Changed += Watcher_Created;
watcher.Path = path;
watcher.Filter = "*.csv";
return watcher;
}
private void Watcher_Created(object sender, FileSystemEventArgs e)
{
System.Threading.Thread.Sleep(1000);
FileInfo fileInfo = new FileInfo(e.FullPath);
if (!IsFileLocked(fileInfo))
{
CheckNumberOfRecordsInFile(e.FullPath);
}
}

FileSystemWatcher and Hidden Share

I have a hidden share:
\\computername\Logs$
And I need to monitor file changes in that share.
I've decided to use FileSystemWatcher Class, but it doesn't raise any events. And it doesn't show any errors to me.
static void Main(string[] args)
{
FileWatcher fw = new FileWatcher(#"\\computername\Logs$", "*.*");
fw.Start();
}
class FileWatcher(string filePath, string mask)
{
FileSystemWatcher watcher;
watcher.Path = filePath;
watcher.Filter = mask;
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
watcher.Changed += Watcher_Changed;
watcher.Error += OnError;
public void Start()
{
watcher.InternalBufferSize = 64 * 1024;
watcher.EnableRaisingEvents = true;
Console.WriteLine("Watcher Started");
while (!Console.KeyAvailable)
{
Thread.Sleep(1000);
}
}
private void Watcher_Changed(object sender, FileSystemEventArgs e)
{
Console.WriteLine("File Changed");
}
private void OnError(object sender, ErrorEventArgs e)
{
Console.WriteLine("Error");
}
}
Does FileSystemWatcher work properly with hidden shares?
This code will not compile ,maybe you had errors while copy the code
and where the event handler Watcher_Changed, maybe you have mistake and put Watcher_Created instead

How do I keep a RichTextBox updated constantly with a TXT file?

I have this code, which should keep richTextBox2 updated at all times with usedPath's contents, but it doesn't.
private void watch()
{
var usedPath = Path.Combine(Directory.GetCurrentDirectory(), "usedwords.txt");
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = usedPath;
watcher.NotifyFilter = NotifyFilters.LastWrite;
watcher.Filter = "*.txt*";
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.EnableRaisingEvents = true;
}
private void OnChanged(object source, FileSystemEventArgs e)
{
string usedPath = Path.Combine(Directory.GetCurrentDirectory(), "usedwords.txt");
richTextBox2.LoadFile(usedPath, RichTextBoxStreamType.PlainText);
}
Can someone help me figure out what I have wrong?
Problem 1: Your watcher.Path = path of a single file, which will cause error.
Solution: Look at this: Use FileSystemWatcher on a single file in C#
watcher.Path = Path.GetDirectoryName(filePath1);
watcher.Filter = Path.GetFileName(filePath1);
Problem 2: Accessing richTextBox2 in OnChanged() will cause cross-thread error
Solution: Use this:
private void OnChanged(object source, FileSystemEventArgs e)
{
Invoke((MethodInvoker)delegate
{
string usedPath = Path.Combine(Directory.GetCurrentDirectory(), "usedwords.txt");
richTextBox2.LoadFile(usedPath, RichTextBoxStreamType.PlainText);
});
}
Problem 3: There may be error when trying to LoadFile while some other programs are writing to it.
(Possible) Solution: Put a Thread.Sleep(10) in before trying to LoadFile in OnChanged
private void OnChanged(object source, FileSystemEventArgs e)
{
Thread.Sleep(10);
Invoke((MethodInvoker)delegate
{
richTextBox1.LoadFile(usedPath, RichTextBoxStreamType.PlainText);
});
}
My complete code:
public partial class Form1 : Form
{
string usedPath = #"C:\Users\xxx\Desktop\usedwords.txt";
public Form1()
{
InitializeComponent();
watch();
}
private void watch()
{
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = Path.GetDirectoryName(usedPath);
watcher.Filter = Path.GetFileName(usedPath);
watcher.NotifyFilter = NotifyFilters.LastWrite;
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.EnableRaisingEvents = true;
}
private void OnChanged(object source, FileSystemEventArgs e)
{
Thread.Sleep(10);
Invoke((MethodInvoker)delegate
{
richTextBox1.LoadFile(usedPath, RichTextBoxStreamType.PlainText);
});
}
}

Filesystemwatcher double entries

I made a small winforms application to monitor a certain folder for new pdf files, if a new pdf file is created in the particulair folder it will copy it to an other location.
The problem i'm having is that the filesystemwatcher creates double/multiple entries in my listbox, how can i solve this?
namespace Scanmonitor
{
public partial class Form1 : Form
{
FileSystemWatcher watcher = new FileSystemWatcher();
DateTime lastRead = DateTime.MinValue;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
FileWatch();
}
public void FileWatch()
{
watcher.Path = #"C:\Scanner";
watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.LastWrite;
watcher.Filter = "*.pdf";
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.EnableRaisingEvents = true;
}
public void OnChanged(object source, FileSystemEventArgs e)
{
scannerListBox.Items.Add(e.FullPath);
scannerListBox.SelectedIndex = scannerListBox.Items.Count - 1;
FileMove(scannerListBox.SelectedItem.ToString());
}
public void FileMove(string filePath)
{
try
{
System.IO.File.Copy(filePath, #"\\share\Data\Scans op OCE 600\" + Path.GetFileName(filePath));
}
catch (Exception ex)
{
ToolLabel.Text = ex.Message.ToString();
}
}
}
}
}
Got it working.
public void OnChanged(object source, FileSystemEventArgs e)
{
try
{
watcher.EnableRaisingEvents = false;
FileInfo objFileInfo = new FileInfo(e.FullPath);
if (!objFileInfo.Exists) return;
System.Threading.Thread.Sleep(5000);
FileInfo fileinformatie = new FileInfo(e.FullPath);
string strCreateTime = fileinformatie.CreationTime.ToString();
string strCreateDate = fileinformatie.CreationTime.ToString();
strCreateTime = strCreateTime.Remove(strCreateTime.LastIndexOf(" "));
strCreateDate = strCreateDate.Remove(0,strCreateDate.LastIndexOf(" "));
ProcessAllFiles(e.FullPath, strCreateTime, strCreateDate);
}
catch (Exception ex)
{
ToolLabel.Text = ex.Message.ToString();
}
finally
{
watcher.EnableRaisingEvents = true;
}
}
You need to keep track of files (in a collection or dictionary) that already raised an event by the FileSystemWatcher. According to MSDN
Common file system operations might raise more than one event. For example, when a file is moved from one directory to another, several OnChanged and some OnCreated and OnDeleted events might be raised. Moving a file is a complex operation that consists of multiple simple operations, therefore raising multiple events. Likewise, some applications (for example, antivirus software) might cause additional file system events that are detected by FileSystemWatcher.
public void OnChanged(object source, FileSystemEventArgs e)
{
try
{
watcher.EnableRaisingEvents = false;
FileInfo objFileInfo = new FileInfo(e.FullPath);
if (!objFileInfo.Exists) return;
System.Threading.Thread.Sleep(5000);
FileInfo fileinformatie = new FileInfo(e.FullPath);
string strCreateTime = fileinformatie.CreationTime.ToString();
string strCreateDate = fileinformatie.CreationTime.ToString();
//Ignore this, only for my file information.
strCreateTime = strCreateTime.Remove(strCreateTime.LastIndexOf(" "));
strCreateDate = strCreateDate.Remove(0,strCreateDate.LastIndexOf(" "));
ProcessAllFiles(e.FullPath, strCreateTime, strCreateDate);
}
catch (Exception ex)
{
ToolLabel.Text = ex.Message.ToString();
}
finally
{
watcher.EnableRaisingEvents = true;
}
}

Categories

Resources