I have this code:
bool containsNonAllowedCLEOFiles = directory.EnumerateFiles().Any(file => !allowedCLEOFiles.Contains(file.Name));
if (containsNonAllowedCLEOFiles == true)
{
DialogResult existsunallowedcleofiles = MessageBox.Show("Extraneous files found! Please remove them", "Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
if(existsunallowedcleofiles == DialogResult.OK)
{
//move files
}
return;
}
If extraneous/unallowed files exist, I'm getting this message. But I want to move those files to another directory when I click OK. How can I do it? I want to move not all files, but only extraneous / unallowed.
P.S I know that I must use File.Move("file", "directory"); or something like this, but I don't know how to get that file name.. etc.
Well it sounds like you should first find those files, then check whether or not there are any:
var invalidFiles = directory.EnumerateFiles()
.Where(file => !allowedCLEOFiles.Contains(file.Name));
.ToList();
if (invalidFiles.Any())
{
// ... Prompt user as before ...
foreach (var invalidFile in invalidFiles)
{
File.Move(...);
}
}
See more on this question at Stackoverflow