c# - Multiple optional checks with Where query (to specify file search options) -
I created the checkbox, where a user can set this file to search:
- System Excluded Files: On / Off
- Skip hidden files: On / Off
- Skip read only files: On / Off
Now I do not know, how can I use these three different Boolean statements with this code line:
DirectoryInfo DirInfo = new directoryInfo (sCopyFilesFromPi LePath); GetDirsToCopy = DirInfo.EnumerateDirectories (".", SearchOption.AllDirectories). where (?);
You can use an implication in the argument a -> ; B translated to ! A || B , like this:
DirInfo.EnumerateDirectories (".", SearchOption.AllDirectories). Where (x => exit system) (x properties and file properties system) == 0); . (Where x => is omitted) (X. Properties and file entries. Hild) == 0); . (Where x = & gt ;! ExcludeReadOnly || (X. Features and file entries, read only) == 0); Here we believe that Excluded Systems , Excluded and excludeReadOnly are bool s
Explanation (which is the first time you received from checkboxes).
You can definitely write a where too. A . Where ):
Looking at the following expression
x = & gt; Excluded Systems || (X. Features and file entries. Systems) == 0 You can read it as a reality Excluded System is false , then ! Exclude System is true , thus all directories will be successful. If not, the second operand (x.Attributes and FileAttributes.System) == 0 is executed.
The second operator is the first and the bitword & amp; Properties on x and continuous file entries. On System If attributes is such as FileAttributes.System , the result will be slightly different from zero. In that case the second exam fails. In other words, if the files are excluded and the file is a system-file, then you return the false .
Optimize
If you think you'll be redirecting thousands of directories, then make the code a little more efficient by making the test faster. Can be:
IEnumerable & lt; DirectoryInfo & gt; Temp = DirInfo.EnumerateDirectories (".", SearchOption.All Directories); If (Excluded Systems) {temp = temp.Where ((X Properties and File Attributes Systems) == 0); } If (left ish) {temp = temp.Where ((X properties and file attributes.) == 0); } If (leave RaidOnly) {temp = temp.Where ((X. Features and file entries. Read only) == 0); } GetDirsToCopy = temp; This (almost) will always be efficient, but it makes the code a little less beautiful.
Comments
Post a Comment