Negate a string in C#

与世无争的帅哥 提交于 2020-01-17 05:42:05

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!