问题
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);
}
回答1:
Try : If(!e.FullPath.EndsWith("temp.temp"))
回答2:
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);
}
回答3:
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);
}
回答4:
If you want to just ignore "temp.temp" how about an early return?
if (e.Name.Equals("temp.temp", StringComparison.CurrentCultureIgnoreCase))
return;
回答5:
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.
来源:https://stackoverflow.com/questions/6384789/negate-a-string-in-c-sharp