I'm trying to detect the temporary .tmp file in the FileSystemWatcher Create event

cblack

Member
Joined
Oct 24, 2015
Messages
5
Programming Experience
1-3
Hi

I'm trying to detect the hidden (although I've seen it for a split second in Windows File Explorer) .tmp file that's created in the FileSystemWatcher Create Event. The .tmp file appears in the same directory as the file that's created. I'm using the example for FileSystemWatcher here:

https://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher(v=vs.110).aspx

In the Create event I find the hidden .tmp file that is automatically generated changes my e.changeType to "Deleted" when it is deleted after a split second. So I'm trying to detect it and stop it from affecting the rest of the code block. I tried this code sample I found on the web.

FileAttributes attr = System.IO.File.GetAttributes(e.FullPath);

FileInfo fi = new FileInfo(e.FullPath);     

if ((attr & FileAttributes.Hidden) == FileAttributes.Hidden || fi.Extension == ".tmp")
{
     MessageBox.Show(".tmp file found");
}


But the first line gives me an error as the hidden .tmp file isn't part of e.FullPath. How can I change this to detect the hidden .tmp file?

Thanks.
 
Last edited:
I'm not sure if it''s going to help you but you can define separate handlers for the different event types

        // Add event handlers.
        watcher.Changed += new FileSystemEventHandler(OnChanged);
        watcher.Created += new FileSystemEventHandler(OnCreated);
        watcher.Deleted += new FileSystemEventHandler(OnDeleted);
        watcher.Renamed += new RenamedEventHandler(OnRenamed);


And add the handlers

    // 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);
    }

private static void OnCreated(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 OnDeleted( 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);
    }


Note that FileAttributes attr = System.IO.File.GetAttributes(e.FullPath); will still throw errors if the file is deleted and therefor no longer exists; there is no way around that.
 
Last edited:
I'm not sure if it''s going to help you but you can define separate handlers for the different event types

I've already done that as per the example I linked too in my original post.

Note that FileAttributes attr = System.IO.File.GetAttributes(e.FullPath); will still throw errors if the file is deleted and therefor no longer exists; there is no way around that.

Is there no way to catch the .tmp file before its deleted? Is there any way to detect it as it caused the Deleted event?

Thanks.
 
Back
Top Bottom