Watching & Printing New Files in a Directory – vb & c#

FileWatcherPrinterMonitoring a folder for new files in .net can easily be watched using the FileSystemWatcher in System.IO. You give it a path (ie: C:\toprint) and it will raise an event when a file is created (you can also watch for deleted, renamed and updated files).

Edit: I have new / full code here on GitHub.

I’ve had the need on several occasions to print new files as they are created, so the combination of the FileSystemWatcher and using a process to call the file and print work well together. Below the code to print to the default printer (works great for .pdf files):


 Process PrintProcess = new Process();
 PrintProcess.StartInfo.CreateNoWindow = false;
 PrintProcess.StartInfo.Verb = "print";
 PrintProcess.StartInfo.FileName = e.FullPath;


 Dim PrintProcess As New Process
 PrintProcess.StartInfo.CreateNoWindow = False
 PrintProcess.StartInfo.Verb = "print"
 PrintProcess.StartInfo.FileName = e.FullPath

The file watcher uses this syntax:


FileSystemWatcher fsWatcher = new FileSystemWatcher(txtDirToWatch.Text);
fsWatcher.Created += OnChanged;
 var withFSW = fsWatcher;
 withFSW.EnableRaisingEvents = true;
 withFSW.IncludeSubdirectories = false;
 withFSW.Filter = "*.pdf";


Dim fsWatcher As New FileSystemWatcher(txtDirToWatch.Text)
AddHandler fsWatcher.Created, AddressOf OnChanged
With fsWatcher
 .EnableRaisingEvents = True
 .IncludeSubdirectories = False
 .Filter = "*.pdf"
End With

And the OnChanged Event: c#:

 public void OnChanged(object source, FileSystemEventArgs e)
   //Do something with e.FullPath, etc.



 Public Sub OnChanged(ByVal source As Object, ByVal e As FileSystemEventArgs)
  'Do something with e.FullPath, etc.
 End Sub


*I haven’t used this method to print many different formats or tons of files at once, but give it a try!

If you’d like a fully working solution (vb or c#) drop me a line at chris.bitting(at)gmail(dot)com and I’d be glad to send my project and code.

Watching & Printing New Files in a Directory – vb & c#

2 thoughts on “Watching & Printing New Files in a Directory – vb & c#

  1. Makis says:

    That was a great code which I took and created my own watcher in vb. I had an issue that every time I run the program, the very first file didn’t work as expected. The solution I found was to change the order of AddHandler and I put it before the the .waitForChange method. So my code looks like:

    AddHandler fsWatcher.Created, AddressOf OnChanged
    With fsWatcher
    .EnableRaisingEvents = True
    .IncludeSubdirectories = False
    .Filter = “*.jpg”

    End With

    After that adjustment everything works like a charm!
    Thank you for your code!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s