0

Im writing a simple folder watcher program, I would like to ignore a temp.temp file that gets copied into the folder when a scan is made, so the program will detect anything placed in the folder and ignore the temp.temp file. At the moment I have got the program detecting IMG files to get around the problem.

if(e.FullPath.Contains("IMG"))            
{ 
    MessageBox.Show("You have a Collection Form: " + e.Name);
    Process.Start("explorer.exe", e.FullPath);
}
6
  • 2
    Is there some reason if(e.FullPath.Contains("temp.temp")) wouldn't be an appropriate test, here?
    – Frosty840
    Jun 17, 2011 at 11:03
  • 5
    What do you mean by "negate" a string? Your question is very unclear.
    – Jon Skeet
    Jun 17, 2011 at 11:03
  • 2
    What exactly does negate mean here??
    – Aliostad
    Jun 17, 2011 at 11:03
  • 2
    I think he means he wants to ignore that particular file. His current solution seems valid to me.
    – user159335
    Jun 17, 2011 at 11:04
  • I need to ignore temp.temp and detect any file that is placed in the folder.
    – Matt
    Jun 17, 2011 at 11:06

5 Answers 5

7

If e is of type FileInfo, then you can use

if(e.FullPath.Contains("IMG") && e.Name.Equals("temp.temp", StringComparison.CurrentCultureIgnoreCase))
{ 
    MessageBox.Show("You have a Collection Form: " + e.Name);
    Process.Start("explorer.exe", e.FullPath);
}
0
2

Try : If(!e.FullPath.EndsWith("temp.temp"))

2
  • That'll also catch babytemp.temp, which as I understand, should not be ignored.
    – foxy
    Jun 17, 2011 at 11:09
  • Yeah ure right it should be changed to equality check if its exact file name that should be ignored Jun 17, 2011 at 11:27
0

So if by 'negate' you mean 'ignore', this should work:

if(Path.GetFileName(e.FullPath) != "temp.temp")            
{ 
    MessageBox.Show("You have a Collection Form: " + e.Name);
    Process.Start("explorer.exe", e.FullPath);
}
0

If you want to just ignore "temp.temp" how about an early return?

if (e.Name.Equals("temp.temp", StringComparison.CurrentCultureIgnoreCase))
    return;
0

If you're using a FileSystemWatcher use the constructor described here http://msdn.microsoft.com/en-us/library/0b30akzf.aspx to filter for files you want rather than negate the ones you don't.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.