S
S
Sergey Korenevsky2022-02-17 15:19:30
.NET
Sergey Korenevsky, 2022-02-17 15:19:30

How to properly compare ENUM in C#?

Help plz.
I have already found Yandex, and stackoverflow and so on. how to do ENUM
checks correctly ?

DirectoryInfo dir = new DirectoryInfo("D:\\");
if (dir.Attributes == (FileAttributes.Hidden & FileAttributes.Offline)) 
{
        return;
}

The bottom line is that when I search for the contents of a folder on subfolders, there are folders with the name D:\$RECYCLE.BIN
And I don’t need these folders, but they cause errors, I also need to cut off hidden folders and system ones like Windows.
In general, when this code is executed, it does not fire on hidden folders.
How to properly compare ENUM ?

I would like to understand the meaning of the operators & and | in this comparison what they do.

Strange affair. For some reason, all my logical drives as folders have attributes
directory.Attributes = Hidden | System | Directory | Archive

How can drive D: have the Hidden attribute?
No wonder my code doesn't work.
Thus, I cannot separate the D:\$RECYCLE.BIN folder from the D:\ folder . They both have the hidden attribute.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
none7, 2022-02-17
@Dier_Sergio_Great

FileAttributes is not an Enum in the usual sense. This is a bit mask, that is, a number in which certain bits have certain values. The example is frankly wrong, since the expected directories will also have the Directory attribute, at least. If you need to find all files that have one of the attributes, then the code should look like this

if(dir.Attributes & (FileAttributes.Hidden | FileAttributes.Offline) != 0)

Here we add the bits of two attributes and if either of them is not equal to 0, then the condition will work.

S
Sergey Korenevsky, 2022-02-17
@Dier_Sergio_Great

Спасибо всем участникам. Кроме вопроса, Вы помогли раскрыть эту тему полностью. Надеюсь это будет полезно ищущим. Собственно я вопрос этот специально писал, чтобы его можно было легко найти через поиск.
Если интересно.
То прога простая, консольная, благодаря Вам ее написал. Показывает список папок и файлов пути которых больше чем 250 символов. Так как злой проводник Windows имеет ограничение для копирования файлов в 250 символов.
Нет возможности делать резервные копии. При больших объемах резервного копирования всегда найдутся 1000 файлов которые выдадут ошибку при копировании.
Надеюсь прога будет полезной.
https://disk.yandex.ru/d/q6YMB4pSRc3Kgw
620ead748c90b952359871.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question